From: Eric Armstrong Date: 2006-07-22T22:07:54+09:00 Subject: Re: recursion in scripts -- global variables required? That's IT. That's precisely what confuses me. The same surface syntax works completely differently in the two different settings. It only makes sense when you understand the deeper model -- and I CONTINUALLY forget that initializing a variable in a class definition only applies to the class objects, rather than instance objects. The same thing came just last week in another way, in fact. It only took a week to forget again! Thanks for the reminder. (But I wonder if there is a way to make things less surprising.) gwtmp01@mac.com wrote: > > On Jul 20, 2006, at 10:05 PM, Eric Armstrong wrote: >>> @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 > > > >