From: Bob Sidebotham Date: 2004-09-24T08:39:34+09:00 Subject: Re: "no block given" with closure? Robert Klemme wrote: > You make it a bit too complicated, IMHO. If you just want to invoke > some block on an Enumerable, then you can have that directly (the yield > block in the first example basically just forwards the element). So you > could do this as well: Thanks Robert (and others) for your help. I had thought that yield somehow dynamically found the first block up the call stack and called that. Evidently, that's wrong, and yield will only call a block directly associated with the method. I don't think I'm making this too complicated. The example I gave was distilled down from some stuff in Kansas. In Kansas, you can write: kh.select(:Customer) { |c| c.cust_id > 100 }.each do |cust| puts "#{cust.cust_name}" end The nested blocks in my original example correspond to the above. In the current implementation of Kansas, the database query is executed all at once, and returns an array of records--that array is then passed back from the block, above, for iteration. I wanted this to be truly iterative so I wrapped up the sql in a class with an each method, as below: class KSSelection def initialize(&doselect) @doselect = doselect end def each(&userblock) @doselect.call(userblock) end end def select(*args) tables,sql,read_only = check_query_args(*args) ... KSSelection.new do |userblock| @dbh.select_all(sql) do |row| userblock.call add_object( tables[0].new.load(row.to_h, self, read_only)) end end end This code works (but of course, it didn't work when I naively used yield, instead of userblock.call, thinking that yield was dynamically scoped). If there's some way to do this without the stub KSSelection class, that'd be really neat, but I don't think there is. Bob P.S. Is there an actual Ruby language reference that would answer questions like this?