From: Robert Klemme Date: 2003-01-21T17:55:48+09:00 Subject: Re: Local variable scope this one works as expected: irb(main):001:0> @@foo = "bar" "bar" irb(main):002:0> def function irb(main):003:1> puts @@foo irb(main):004:1> end nil irb(main):005:0> function bar nil irb(main):006:0> Note the "@@" which makes "foo" a module variable in the current module, which thus can be seen by method "function". Regards robert "Phil Tomson" schrieb im Newsbeitrag news:b0hori02poj@enews2.newsguy.com... > In article <20030120193158.GA1029@math.umd.edu>, > Daniel Carrera 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 # > > 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