assign blocks to variables

P

poseid

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
}
}
 
J

James Harrison

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:
=20
myfunc {
otherfunc { |a|
print a
}
}
=20
=20


You hand blocks in as a variable to a function by prepending a variable =
name with an ampersand:

myfunc(&block)

Is that what you're looking for?=
 
P

Phrogz

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:

N is 42
I said, it is 42
Oh my 42
 
M

Marvin Gülker

poseid said:
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".
 
P

poseid

#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.

Thank you all.
#proc is solving my problem!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top