Issue #16414 has been updated by jeremyevans0 (Jeremy Evans).


I agree this is a bug.  The with_index block should be called with two arguments, not an array with one argument.

The reason this happens is that `Enumerator::Lazy#with_index` ends up calling `Enumerator::Yielder#<<` (`yielder_yield_push` C function) on the yielder with an array with two entries.  That in turn calls `rb_proc_call_with_block(ptr->proc, 1, &arg, Qnil);`.  This is why the block gets called with a single array argument.

After experimenting more, `Enumerator::Lazy#with_index` has other issues.  The most critical is it ignores the block passed to it.  I'm fairly sure it needs to be rewritten.  I'll see if I can fix the problems with it.  If the problems can't be fixed, we should probably back out the changes, so that `Enumerator::Lazy#with_index` is no longer lazy.

----------------------------------------
Bug #16414: Incompatible behavior of Proc/lambda with single argument when using `Enumerator::Lazy#with_index`
https://bugs.ruby-lang.org/issues/16414#change-83074

* Author: tomog105 (Tomohiro Ogoke)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.7.0dev (2019-12-10T10:12:21Z master af11efd377) [x86_64-darwin18]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
The following code raised an error `wrong number of arguments (given 1, expected 2) (ArgumentError)` in master.

```
$ ruby -e 'lambda = -> (s, i) { "#{i}:#{s}" }; p %w(a b c).each.lazy.with_index.map(&lambda).first(2)'
# expected result => ["0:a", "1:b"]
```

This code is valid up till Ruby 2.6.5 and 2.7.0-preview1, but it raised the error in Ruby 2.7.0-preview2 or later. Maybe, this behavior has been there since the implementation of `Enumerator::Lazy#with_index`.

Is this behaviour intended?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>