如何将 lambda 传递给hash.each
,以便我可以重用一些代码?
> h = { a: 'b' }
> h.each do |key, value| end
=> {:a=>"b"}
> test = lambda do |key, value| puts "#{key} = #{value}" end
> test.call('a','b')
a = b
> h.each &test
ArgumentError: wrong number of arguments (1 for 2)
from (irb):1:in `block in irb_binding'
from (irb):5:in `each'
from (irb):5
from /Users/jstillwell/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `<main>'
> h.each test
ArgumentError: wrong number of arguments(1 for 0)
from (irb):8:in `each'
from (irb):8
from /Users/jstillwell/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `<main>'