In article <20030120193158.GA1029 / math.umd.edu>,
Daniel Carrera  <dcarrera / math.umd.edu> wrote:
>I'm a bit confused by the scope of local variables.
>
>$ irb --simple-pompt
>>> foo = "bar" 
>=> "bar"
>>> def function
>>>     puts foo
>>> end
>=> nil
>>> function
>NameError: undefined local variable or method `foo' for #<Object:0xf9000>
>        from (irb):3:in `function'
>        from (irb):5
>
>Since 'foo' was defined before 'function' and both 'function' and 'foo' 
>are in the same scope (I think) shouldn't 'function' see 'foo'?

That's kind of how Perl's scoping works, but not Ruby.  The scope inside 
your def function is different from the scope outside.  So inside of 
method 'function' there is no 'knowledge' of the foo variable that was 
defined outside.

Phil
-- 
"Or perhaps the truth is less interesting than the facts?" 
Amy Weiss (accusing theregister.co.uk of engaging in 'tabloid journalism')
Senior VP, Communications
Recording Industry Association of America