Bug #16748
closedDifferent behaviour between a hash and multi-Array when passing 2 arguments to a proc
Description
In Ruby 2.5.5 i get what i'm expecting
test = -> (one, two) { one }
[['one', 'two']].map(&test)
=> ["one"]
In Ruby 2.7.0 this doesn't work
test = -> (one, two) { one }
[['one', 'two']].map(&test)
wrong number of arguments (given 1, expected 2) (ArgumentError)
Files
Updated by Mattruby (Matthew Nash) over 5 years ago
Updated by mame (Yusuke Endoh) over 5 years ago
- Description updated (diff)
Updated by mame (Yusuke Endoh) over 5 years ago
- Status changed from Open to Rejected
The behavior of 2.5.5 was wrong. Array#map yields each element, in this case, ['one', 'two'], an array of two elements. The lambda requires two arguments, but one array is passed, which should raise "wrong number of arguments".
You may want to use -> (ary) { ary[0] } instead of -> (one, two) { one }. Or you can use proc {|one, two| one }.
Updated by Mattruby (Matthew Nash) over 5 years ago
mame (Yusuke Endoh) wrote in #note-3:
The behavior of 2.5.5 was wrong.
Array#mapyields each element, in this case,['one', 'two'], an array of two elements. The lambda requires two arguments, but one array is passed, which should raise "wrong number of arguments".You may want to use
-> (ary) { ary[0] }instead of-> (one, two) { one }. Or you can useproc {|one, two| one }.
Ok, the strange thing is it works for a Hash, and i assumed that map treats hashes and multi dimensional arrays in the same way.
Thanks though