From: Cleasai Beag Date: 2007-08-15T09:09:47+09:00 Subject: Re: Can a lambda or Proc object yield a value to a block? Logan Capaldo wrote: > You actually can use yield in a block, you just need to realize it's the > yield of the enclosing scope: > > def example > a_proc = lambda { yield "Hi" } > > if block_given? > a_proc.call > end > end > > example { |x| puts x } Thanks for the reply Logan. Obviously I'll have to study Ruby's scope rules a bit more. I notice that I can't modify your example as follows to pass in a lambda object because I get the "LocalJumpError: no block given" error again. a_proc = lambda { yield "Hi" } def example2(proc) if block_given? proc.call end end example2(a_proc) { |x| puts x } Presumably the enclosing scope for the lambda object in my example is outside the scope of the example2 method. It's scope is the scope in which it was defined which is "nowhere" really i.e. it's outside a class, module and method. Presumably because of this it just can't yield a value. Does this sound plausible at all? P.S. What I'm really trying to do is emulate some of things that I've done before in Scheme. For example a generic "each" maker which doesn't work 'cos of the whole LocalJumpError thing. def makeEach(nextValue, start, stop) lambda do x = start while x <= stop yield x x = nextValue.call(x) end end end from11to20 = makeEach(lambda {|x| x+1}, 11, 20) from11to20 {|x| puts x*x} -- Posted via http://www.ruby-forum.com/.