mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Fix Enumerable#cycle; ref #1933
* add multi value support * `each` method may not rewind the sequence so that `cycle` should save elements
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user