On Sunday 23 November 2003 08:26 pm, Mathieu Bouchard wrote:
> Actually, looking at http://www.rubygarden.org/ruby?Rite, I can only see a
> change in the opposite direction, block locals, and although I can't
> justify that change either, at least it doesn't have as much of a
> compatibility issue.
>
> What confuses me most in all of this is, why the change towards a flat
> local space isn't included in Matz' list, and why an opposite idea is
> included in Matz' list. I also feel that the item in matz' list is not
> completely clear, and suspect that I don't really understand it.
>

Lets clear this up, b/c i've read that too, and became very confused for a 
while. I do not believe that page has a proper explination and makes you 
think that the blocks are somehow fully isolated, but they are not. So...

Here is the current behavior:

  a = [1,2,3]
  i = nil
  a.each { |x| i = x + i.to_i }
  p "#{i}"  # => "6"

The i = nil is required prior to the block in order for i to "persist" in the 
outer scope as it passes through the inner scope of the block. Without it the 
last statement would produce:

  p "#{i}"  # => ""

Now the newly proposed behavior would allow us to remove the i = nil.

  a = [1,2,3]
  a.each { |x| i = x + i.to_i }
  p "#{i}"  # => "6"

So i exists at both levels.

Basically what is happening is that blocks no longer represent divisions of 
scope. Those barriers are now only on def, class, module, and the TOPLEVEL.  
So we are loosing a mechinism of inner scoping, albiet a limted one.

This WILL brake code. Consider:

  a = [1,2,3]
  a.each { |x| i = x + i.to_i }
  p "#{i}"  # => "6"

  a = [1,2,3]
  a.each { |x| i = x + i.to_i }
  p "#{i}"  # => "12"  # => Doh! It's not 6 anymore!

If all Matz wants to do is get rid of the "silly" i=nil, so the code looks 
cleaner then he should try something like:

  a = [1,2,3]
  a.each { i |x| i = x + i.to_i }
  p "#{i}"  # => "6"

See the difference? And no "back" breaking. But maybe he's after something 
else. I don't know.

-t0