foo = lambda do |a,b,c|
Placing an ampersand in front of an element in a method call, for example. a.map!(&:to_i) calls the to_proc method on this object and passes the resulting proc as a block. Some alternative forms of defining your reusable block:
foo = Proc.new{ |a,b,c| ... } foo = proc{ |a,b,c| ... } foo = ->(a,b,c){ ... }
If you call a method with a block, and you want to save this block for reuse later, you can do this by using the ampersand in the method definition to capture the block as the proc parameter:
class DoMany def initialize(*items,&block) @a = items @b = block
Phrogz
source share