Skip to content

Commit

Permalink
Fix Enumerator#each_with_index with block
Browse files Browse the repository at this point in the history
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]
```
  • Loading branch information
okkez committed Mar 30, 2017
1 parent 905f46a commit d93a5c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mrbgems/mruby-enumerator/mrblib/enumerator.rb
Expand Up @@ -177,8 +177,8 @@ def with_index(offset=0)
#
# If no block is given, a new Enumerator is returned that includes the index.
#
def each_with_index
with_index
def each_with_index(&block)
with_index(0, &block)
end

##
Expand Down
7 changes: 7 additions & 0 deletions mrbgems/mruby-enumerator/test/enumerator.rb
Expand Up @@ -65,6 +65,13 @@ def s.to_int; 1 end
assert_raise(TypeError){ @obj.to_enum(:foo, 1, 2, 3).with_index('1').to_a }
end

assert 'Enumerator#each_with_index' do
assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).each_with_index.to_a)
a = []
@obj.to_enum(:foo, 1, 2, 3).each_with_index {|*i| a << i}
assert_equal([[1, 0], [2, 1], [3, 2]], a)
end

assert 'Enumerator#with_object' do
obj = [0, 1]
ret = (1..10).each.with_object(obj) {|i, memo|
Expand Down

0 comments on commit d93a5c1

Please sign in to comment.