From: Alexander Schofield Date: 2001-11-03T14:14:46+09:00 Subject: [ruby-talk:24249] Re: "yield called out of block" "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]] Because it is easier to remove structure later on then it is to impose it, and because you could then take this resulting array and use it in much the same way you would have if you had supplied a block in the first place, even if it's not as flexible as supplying a block and building up the structure you want, the hope is that it would save you typing in most (but it wouldn't be suitable for all) cases. David Alan Black wrote: > def multiple > return 1,2,3 > end > a, = multiple # a is now 1 Good to know, and I withdraw any thoughts about the benefits of real multiple return values based on this (see above). > 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. > 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 ?