From: Logan Capaldo Date: 2006-09-11T11:48:59+09:00 Subject: Re: Struggling with Blocks On Mon, Sep 11, 2006 at 11:15:23AM +0900, Paul Lutus wrote: > 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. > That's a bit of a stretch, don't you think? What exactly is in "the stream"? I can pretty much gurantee you that Dir.chdir looks like: def Dir.chdir(directory) old_directory = Dir.pwd begin chdir_without_block(directory) yield ensure chdir_without_block(old_directory) end end I see state, but no stream. What about instance_eval? That's not a "stream" is it? Heck, lambda { |x, y| x + y }.call(1, 2), where's the stream in that?