List Info

Thread: Subtle difference between m { |arg| do_something(arg) } and m(&method(:do_something))




Subtle difference between m { |arg| do_something(arg) } and m(&method(:do_something))
country flaguser name
France
2007-02-28 04:46:59
[~/tmp]% cat test.rb
a = [[1, 2], [3, 4]]

def max(values)
    values.max
end

puts a.map { |args| max(args) }.join(", ")
puts a.map(&method(:max))

[~/tmp]% ruby test.rb
2, 4
test.rb:9:in `max': wrong number of arguments (2 for 1)
(ArgumentError)
        from test.rb:9:in `to_proc'
        from test.rb:8:in `map'
        from test.rb:8

In the 'method' case, the array is splat without asking
first. Is it expected 
behaviour ? Why ?
-- 
Sylvain



[1]

about | contact  Other archives ( Real Estate discussion Medical topics )