I’ve searched quite a bit on the web and through “Programming Ruby - the
Pragmatic Programmer’s Guide” and I can’t find a satisfactory answer to
the question:
Can a lambda or Proc object yield a value to a block?
For example when I try this
yieldArg = lambda {|arg| yield arg}
yieldArg.call(23) {|y| puts(“y=”+y.to_s)}
in Interactive Ruby I get
“LocalJumpError: no block given”
So I try
yieldArg = lambda {|arg| puts “no block given” unless block_given? ;
yield arg}
to print some indication of whether the block really has been “given”
and I get
no block given
LocalJumpError: no block given
So then I try to wrap the block in a lambda and pass it as a param
putsY = lambda {|y| puts(“y=”+y.to_s)}
yieldArg.call(23) {|y| puts(“y=”+y.to_s)}
yieldArg.call(23, &putsY)
but with the same negative result.
So can a lambda or Proc object yield a value to a block? If so, how is
it done?
Thanks in advance.
Interactive Ruby session shown below.
irb(main):001:0> yieldArg = lambda {|arg| yield arg}
=> #Proc:0x02dfebf0@:1(irb)
irb(main):002:0> yieldArg.call(23) {|y| puts(“y=”+y.to_s)}
LocalJumpError: no block given
from (irb):1
from (irb):2:in call' from (irb):2 irb(main):003:0> yieldArg = lambda {|arg| puts "no block given" unless block_given? ; yield arg} => #<Proc:0x02df58e8@(irb):3> irb(main):004:0> yieldArg.call(23) {|y| puts("y="+y.to_s)} no block given LocalJumpError: no block given from (irb):3 from (irb):4:in
call’
from (irb):4
irb(main):005:0> putsY = lambda {|y| puts(“y=”+y.to_s)}
=> #Proc:0x02dede2c@:5(irb)
irb(main):006:0> yieldArg.call(23, &putsY)
no block given
LocalJumpError: no block given
from (irb):3
from (irb):6:in `call’
from (irb):6
irb(main):007:0>