Project

General

Profile

Actions

Bug #16748

closed

Different behaviour between a hash and multi-Array when passing 2 arguments to a proc

Bug #16748: Different behaviour between a hash and multi-Array when passing 2 arguments to a proc

Added by Mattruby (Matthew Nash) over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:97660]

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

Screen Shot 2020-04-01 at 16.47.37.png (205 KB) Screen Shot 2020-04-01 at 16.47.37.png Mattruby (Matthew Nash), 04/01/2020 03:48 PM

Updated by mame (Yusuke Endoh) over 5 years ago Actions #2

  • Description updated (diff)

Updated by mame (Yusuke Endoh) over 5 years ago Actions #3 [ruby-core:97662]

  • 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 Actions #4 [ruby-core:97665]

mame (Yusuke Endoh) wrote in #note-3:

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 }.

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

Actions

Also available in: PDF Atom