"Bob Sidebotham" <bob / windsong.bc.ca> schrieb im Newsbeitrag news:f5J4d.83622$%S.11627 / pd7tw2no... > 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: Ah, I see. Sounds reasonable especially for large results. > 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. I dunno Kansas so this might not work or might not do what you want. But what about this simple passing on of the block?: def select(*args, &b) ... @dbh.select_all(sql, &b) end > Bob > > P.S. Is there an actual Ruby language reference that would answer > questions like this? There's a pickaxe online version as well as a comprehensive lib documentation reachable via http://www.ruby-doc.org/ Regards robert