fix return value for with_index

This commit is contained in:
ksss
2014-03-14 23:51:39 +09:00
parent bc63518fa7
commit 47fc784b56
2 changed files with 12 additions and 3 deletions
@@ -156,10 +156,10 @@ class Enumerator
return to_enum :with_index, offset unless block_given?
raise TypeError, "no implicit conversion of #{offset.class} into Integer" unless offset.respond_to?(:to_int)
n = offset.to_int
each do |i|
yield [i,n]
n = offset.to_int - 1
enumerator_block_call do |i|
n += 1
yield [i,n]
end
end
@@ -396,3 +396,12 @@ assert 'modifying existing methods' do
assert_equal Enumerator, {a:1}.each.class
assert_equal Enumerator, (1..5).each.class
end
assert 'Array#map!' do
a = [1,2,3]
b = a.map!
b.with_index do |i, index|
[i*i, index*index]
end
assert_equal [[1,0],[4,1],[9,4]], a
end