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?
> In other words, how would you parameterize this in irb:
> 
> myfunc {
>                   otherfunc {  |a|
>                        print a
>                    }
> }

You can assign blocks to variables like this:
irb(main):001:0> b1 = lambda{puts "hello"}
=> #<Proc:0x00000001d83b18@(irb):1 (lambda)>
irb(main):002:0> b1.call
hello
=> nil
irb(main):003:0> b2 = proc{puts "hello"}
=> #<Proc:0x00000001d4de88@(irb):3>
irb(main):004:0> b2.call
hello
=> nil
irb(main):005:0>

Notice that there's a subtle difference between #proc and #lambda if the 
block takes parameters:

irb(main):005:0> b3 = lambda{|arg| p arg}
=> #<Proc:0x00000001d357d8@(irb):5 (lambda)>
irb(main):006:0> b4 = proc{|arg| p arg}
=> #<Proc:0x00000001d1fd20@(irb):6>
irb(main):007:0> b3.call(1)
1
=> 1
irb(main):008:0> b4.call(1)
1
=> 1
irb(main):009:0> b3.call
ArgumentError: wrong number of arguments (0 for 1)
  from (irb):9:in `call'
  from (irb):9
  from /home/marvin/Programmieren/Programme/irb_/irb_.rb:37:in `<main>'
irb(main):010:0> b4.call
nil
=> nil
irb(main):011:0>

#lambda acts more like a method, checking wheather it was given all 
necessary arguments, whereas #proc just assigns nil to parameters that 
weren't provided.

However, to write a function that takes a block you don't have to take 
care about that[*]. There's the "yield" keyword that invokes a supplied 
block and raises a LocalJumpError if no block was supplied.

irb(main):011:0> def foo
irb(main):012:1> yield
irb(main):013:1> end
=> nil
irb(main):014:0> foo{puts "Hello, World!"}
Hello, World!
=> nil
irb(main):015:0>

Additionally, with #block_given? you can check wheather a block has been 
supplied.

Marvin

[*] You could define a method like as "def foo(&block)", than you get a 
#proc-ed block in the variable "block".
-- 
Posted via http://www.ruby-forum.com/.