summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-03-24 23:58:21 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-03-24 23:58:21 +0900
commitd7d54b6e69be83a8905fd49d1d190bf7234424a7 (patch)
tree29e3df3a30aeb146d7adc66f8096969269005d4f
parent915b158e89e173b396f8d71aa03584da24386286 (diff)
downloadmruby-d7d54b6e69be83a8905fd49d1d190bf7234424a7.tar.gz
mruby-d7d54b6e69be83a8905fd49d1d190bf7234424a7.zip
Fix Enumerable#cycle; ref #1933
* add multi value support * `each` method may not rewind the sequence so that `cycle` should save elements
-rw-r--r--mrbgems/mruby-enum-ext/mrblib/enum.rb22
1 files changed, 19 insertions, 3 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb
index b492ae9af..50eaa6a4d 100644
--- a/mrbgems/mruby-enum-ext/mrblib/enum.rb
+++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -548,14 +548,30 @@ module Enumerable
#
def cycle(n=nil, &block)
+ ary = []
if n == nil
- loop {self.each {|val| block.call(val) } }
+ self.each do|*val|
+ ary.push val
+ block.call(*val)
+ end
+ loop do
+ ary.each do|e|
+ block.call(*e)
+ end
+ end
else
- raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer
+ unless n.kind_of? Integer
+ raise TypeError, "expected Integer for 1st argument"
+ end
+ self.each do|*val|
+ ary.push val
+ end
count = 0
while count < n
- self.each {|val| block.call(val) }
+ ary.each do|e|
+ block.call(*e)
+ end
count += 1
end
end