each returns the current element in a block, ergo lambda should take one argument, not two. In the case of Hash resulting element is a two-element Array , with the first element being the key and the second element being the value.
test = lambda do |el| puts "#{el.first} = #{el.last}" end h.each &test
Alternatively, you can use Ruby support to destruct the binding in parameter lists:
test = lambda do |(k, v)| puts "#{k} = #{v}" end h.each &test
Or instead of using lambda , which has the same argument as binding semantics as a method, you can use Proc , which has the same argument as binding semantics as a block, that is, it decompresses one Array into several parameter bindings:
test = proc do |k, v| puts "#{k} = #{v}" end h.each &test
source share