<dblack / wobblini.net> wrote in message 
news:Pine.LNX.4.64.0608011027090.9607 / rubypal.com...
>
>>    Is this an example of "flat" scope?
>
> Yes.
>
>> a = "foo"
>> b = a + "bar"
>> puts b
>>
>>
>>    It may look "flat" but scopes have come and gone.
>
> I only see one local scope there.  Where do you see others?

    The scope in the method "+" came and went.  The scope in the method 
"puts" came and went as well...


>>    Your example took a block and sent it to the method "each," which has
>> it's own scope, where the block would have ordinarily had no hope of
>> accessing the variable "a" except that it was a closure...
>
> The part I'm not convinced of is "sent".  It provided a code block,
> but it didn't send it.

    It got "sent" as much as anything gets sent around here (in Ruby)...
    I already posted this somewhere else on this thread but I guess I'll 
post it, again, here.  The "each" method in class "Array" might have been 
implemented something like this:


class Array
    def each
        i = 0
        while i < self.size
            # this is where the block is executed
            # far away from the scope where "a" is defined...
            # the block was "sent" here...
            yield self[i]

            i += 1
        end
    end
end

# this is the sample code you provided...

a = 10
# the block is defined here but "sent" to the "each" method...
[1, 2, 3].each {|x| puts x * a }


    Of course, I doubt anyone would actually implement "Array#each" like 
that.  They might do something like this:


class Range
    def each
        i = first
        while i <= last
            yield i

            i += 1
        end
    end
end

class Array
    def each
        (0..(size - 1)).each { |i| yield self[i] }
    end
end

# again, this is the sample code you provided...

a = 10
[1, 2, 3].each {|x| puts x * a }


>>    Incidentally, having variables created inside the block be in scope 
>> when
>> the block exits is planned for Ruby2.  Indeed, if memory serves me, it is
>> Matz's "most regretting" design decision that it doesn't already do 
>> this...
>> So, I guess it will then be "flat."
>>    Personally, I look forward to this feature...
>
> I'm among the few that like it the way it is, but I think we're on the
> losing end of this decision-making process for 2.0 :-)

    There was a decision-making process?