From: Phrogz Date: 2010-07-26T00:40:05+09:00 Subject: Re: assign blocks to variables On Jul 25, 8:58 am, poseid wrote: > If you want to work on a function that takes a block as input, is it > possible to assign the block to a variable? You can capture a block parameter passed to a method using an ampersand name at the end. You can pass a proc as a block to a method by throwing an ampersand before it. There are also other ways to create a proc value, such as using the 'proc' or 'lambda' methods, or the arrow syntax available in 1.9: >> def capture_block(&my_block); my_block; end >> def with42; yield 42; end >> b1 = capture_block{ |n| puts "N is #{n}" } => # >> with42( &b1 ) N is 42 >> b2 = proc{ |n| puts "I said, it is #{n}" } => # >> with42( &b2 ) I said, it is 42 >> b3 = ->(n){ puts "Oh my #{n}" } => # >> with42( &b3 ) Oh my 42