Skip to content

Commit d93a5c1

Browse files
committed
Fix Enumerator#each_with_index with block
In previous version, ``` a = [3, 2, 1] e = a.each e.sort_by(&:to_i) # => [] ``` In this version, ``` a = [3, 2, 1] e = a.each e.sort_by(&:to_i) # => [1, 2, 3] ```
1 parent 905f46a commit d93a5c1

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Diff for: mrbgems/mruby-enumerator/mrblib/enumerator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def with_index(offset=0)
177177
#
178178
# If no block is given, a new Enumerator is returned that includes the index.
179179
#
180-
def each_with_index
181-
with_index
180+
def each_with_index(&block)
181+
with_index(0, &block)
182182
end
183183

184184
##

Diff for: mrbgems/mruby-enumerator/test/enumerator.rb

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def s.to_int; 1 end
6565
assert_raise(TypeError){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a }
6666
end
6767

68+
assert 'Enumerator#each_with_index' do
69+
assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).each_with_index.to_a)
70+
a = []
71+
@obj.to_enum(:foo, 1, 2, 3).each_with_index {|*i| a << i}
72+
assert_equal([[1, 0], [2, 1], [3, 2]], a)
73+
end
74+
6875
assert 'Enumerator#with_object' do
6976
obj = [0, 1]
7077
ret = (1..10).each.with_object(obj) {|i, memo|

0 commit comments

Comments
 (0)