This is the syntax of the Ruby block (Ruby name for anonymous function). And key, value- this is nothing but the arguments passed to the anonymous function.
Hash#eachtakes one parameter: a function that has 2 parameters, keyand value.
, , : h.each each h. :
do |key, value|
print "#{value}:#{key}; "
end
- , each , key, value - , . , , , , , - .
. :
def name_of_function(arg1, arg1)
end
foo.name_of_function bar, baz
ooga = lambda { |arg1, arg2|
}
ooga = lambda do |arg1, arg2|
end
ooga.call(bar, baz)
, :
print_key_value = lambda{|arg1, arg2| print "#{arg1}:#{arg2}"}
h = {
:one => 1,
:two => 2
}
h.each &print_key_value
:
yield
yield key, value
yield item
block.call
block.call(key, value)
block.call(item)