From: David Alan Black Date: 2001-11-03T18:43:30+09:00 Subject: [ruby-talk:24256] Re: "yield called out of block" Hello -- On Sat, 3 Nov 2001, Alexander Schofield wrote: > "Hal E. Fulton" wrote: > > > This is an interesting idea. I'm uncertain yet > > as to whether I like it. Let me make sure I > > understand you. > > > > When we iterate over a list of 20 items and > > yield them one at a time (without a block), > > do we get an array back? What if we yield > > them in pairs, i.e. not one at a time? > > Based on the syntactic little gem David Alan Black enlightened me with, > I would say yes, return an array, there's no need for anything like the > multiple return values of CL. > > If pairs are yielded, then I would say impose structure, so: > yield 1,2 > yield 3,4 > without a block would return: > [[1,2],[3,4]] But you really do get into the problem that Eli Green mentioned, namely that you're then storing all these things rather than iterating over them. That could be very costly. Also, what do you mean by "return"? Methods still need to have a return value. For example: def thing yield 123 return 456 end In the absence of a block, what would happen to that 123? I think that's the problem you're touching on here: > > I have a conceptual problem with grasping what > > you're suggesting. The thing is, yield() yields control to a block, > > and the call to yield evaluates to the return value of that block. So > > what exactly could it mean to yield not to a block? What would > > control be being yielded to? > > Perhaps it could be thought of as an implicit block that builds up an > array, even if no prebuilt block would work, since if some structure is > to be imposed you need to know how many args yield was called with. To > say nothing of how this imaginary block would know when to return the > built up array, or even where to return the array to, but then again, > it's only a thought-aid. ... but I'm not sure what it all means if it's just a thought-aid :-) > > def ytest > > return 123 > > end > > > > ytest do |x| print x end # prints 123 > > > > def ytest > > yield(456) > > return 123 > > end > > n = ytest do |x| print x end # prints 456 > > print n # prints 123 > > That's a very interesting idea, but then what if you had: > def ytest > return [1,2,3].each > end > ? You'd get "yield called out of block", or whatever the new wording may be :-) (That would come from the call to #each.) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav