On Jul 20, 2006, at 10:05 PM, Eric Armstrong wrote: > Sean O'Halpin wrote: >> On 7/20/06, Eric Armstrong <Eric.Armstrong / sun.com> wrote: >>> The question is really about the nature of >>> instance variables in a script, I guess. >>> It seems they can only be initialized in a >>> method. >> Not so. Try this: >> @foo = 1 >> def bar >> p @foo >> end >> bar >> #=> 1 > I must be going out of my mind. I can't > tell you how many times that has seemed > to fail... At the moment, of course, it's > working fine. So either something is > failing in some strange intermittent way > (unlikely) or something is confusing the > heck out of me (very likely). Be careful here. The top-level scope in Ruby is not the same thing as class level scope: p self # 1) top_level object def foo p self # 2) top_level object end foo class A p self # 3) the class object, A def a_foo p self # 4) an instance of A end end A.new.a_foo So instance variables in scopes 1 and 2 are actually associated with the same object (the top_level object) while instance variables in 3 and 4 are associated with different objects (the class A and an instance of the class A). Gary Wright