From: Paul Lutus Date: 2006-09-11T11:15:23+09:00 Subject: Re: Struggling with Blocks Hal Fulton wrote: > Paul Lutus wrote: >> >> "do" reads from a list of items and provides them to its controlling >> block: >> >> These two forms are interchangeable. One can argue that "do" is an >> implicit "{ ... }" block, or the reverse. But the point is "do" receives >> items and operates on them one at a time, then exits when its stream is >> empty. > > Not strictly true. Yes, strictly true, IMHO. > Reemember that the iteration comes from the fact > that the each method is invoking the block multiple times. Not if there is only one item. array = [] array << "one item" array.each do |item| # one item end > > Here's a do/end that will only execute once (and by the way > has no parameters): > > Dir.chdir(some_dir) do process_something end In this example, "do" acts on its stream until it is empty. The stream happens to empty after one item, because that is what the stream contains. > And by the way, the rescue that can appear between def and end > is rather an example of what some people want, IIUC. My impression > is that do/end don't have a rescue basically to avoid unneeded > complexity and because putting rescue inside {} for orthognality > looks questionable. Yep. In any case, one can get the rescue within the do ... end block by enclosing a begin .. end block. I don't think this will satisfy anyone who would like to see "rescue" added to do ... end. I've believed for a long time that a "good" language has a minimum number of constructs. -- Paul Lutus http://www.arachnoid.com