Passing a lambda as a block

Tack an ampersand (&) onto the argument, for example:

("A".."K").each &procedure

This signifies that you’re passing it as the special block parameter of the method. Otherwise it’s interpreted as a normal argument.

It also mirrors they way you’d capture and access the block parameter inside the method itself:

# the & here signifies that the special block parameter should be captured
# into the variable `procedure`
def some_func(foo, bar, &procedure)
  procedure.call(foo, bar)
end

some_func(2, 3) {|a, b| a * b }
=> 6

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)