fix self modifying bug

This commit is contained in:
ksss
2014-03-15 00:03:53 +09:00
parent 47fc784b56
commit 6b3cc0052d
2 changed files with 24 additions and 2 deletions
@@ -255,6 +255,7 @@ class Enumerator
# enum.each(:y, :z) { |elm| elm } #=> :method_returned
#
def each *argv, &block
obj = self
if 0 < argv.length
obj = self.dup
args = obj.args
@@ -264,9 +265,9 @@ class Enumerator
else
args = argv.dup
end
@args = args
obj.args = args
end
return self unless block_given?
return obj unless block_given?
enumerator_block_call(&block)
end
@@ -89,6 +89,27 @@ assert 'Enumerator#each' do
assert_equal([1], ary)
end
assert 'Enumerator#each arguments' do
obj = Object.new
def obj.each_arg(a, b=:b, *rest)
yield a
yield b
yield rest
:method_returned
end
enum = obj.to_enum :each_arg, :a, :x
assert_equal [:a, :x, []], enum.each.to_a
assert_true enum.each.equal?(enum)
assert_equal :method_returned, enum.each { |elm| elm }
assert_equal [:a, :x, [:y, :z]], enum.each(:y, :z).to_a
assert_false enum.each(:y, :z).equal?(enum)
assert_equal :method_returned, enum.each(:y, :z) { |elm| elm }
end
assert 'Enumerator#next' do
e = 3.times
3.times { |i|