From d7d54b6e69be83a8905fd49d1d190bf7234424a7 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 24 Mar 2014 23:58:21 +0900 Subject: Fix Enumerable#cycle; ref #1933 * add multi value support * `each` method may not rewind the sequence so that `cycle` should save elements --- mrbgems/mruby-enum-ext/mrblib/enum.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'mrbgems/mruby-enum-ext/mrblib/enum.rb') 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 -- cgit v1.2.3