From: Brian Schroeder Date: 2004-09-24T02:29:35+09:00 Subject: Re: "no block given" with closure? On Thu, 23 Sep 2004 17:09:43 +0000, Bob Sidebotham wrote: > When I execute this code, I get "no block given (LocalJumpError)": > > class S > def initialize(&b1) > @b1 = b1 > end > def each > @b1.call > end > end > > s = S.new {[1,2,3].each {|i| yield i }}.each {|v| puts v} > > Changing the implied block/yield combination to an explicit proc object > works, as follows: > > class S > def initialize(&b1) > @b1 = b1 > end > def each(&b2) > @b1.call(b2) > end > end > > s = S.new { |p| [1,2,3].each { |i| p.call(i) }}.each {|v| puts v} > > This prints the expected 3 lines. > > This is with ruby 1.8.2 (2004-09-10) [i686-linux] > > It's quite possible that I don't understand how this is supposed to work > :-). Any hints would be appreciated. > > Thanks, > Bob Sidebotham I'd say, that as first closure @b1 does not have any block when it is instantiated, so I't won't know of the block given to each. Therefore "no block given". On the other hand in the second example you stuff the block into the first closure, such that it knows about the block. So the bottom line would be: a closure remembers the state at its creation, but it does not know about the state when it is called. I hope it is correct and understandable. regards, Brian Schræ—¦der -- Brian Schræ—¦der http://www.brian-schroeder.de/