Q:

Ruby Scope Iterator

this example:
def test(name)
    names = %w(one two three four)
    names.each do |name|
        puts name
    end
    puts name
end

test 'zero'

#out
one
two
three
four
four  #this is correct? should be zero

is correct the output of this program?
thanks and sorry for my English

A:

This is expected behavior in Ruby 1.8, but from Ruby 1.9.x, blocks variables have scope local to the block.
In Ruby 1.8, the block-variable ends up changing the global binding, if present. That's why, it changes the value of name variable. 
As per, Ruby 1.9.x, the block-variable never changes it's container binding like this. Output:

> test('zero')
  one
  two
  three
  four
  zero  

Consider this example:
x = 10
(1..10).each { |y| print x+y, " "}
#=> 11 12 13 14 15 16 17 18 19 20

Here, x is not the block variable inside the block, so it picks up the binding present outside and uses that value.

