From: anne001 Date: 2006-07-10T08:45:11+09:00 Subject: Re: why does block know about one instance variable and not the other? Thank you for your response. It did occured to me sometime after I wrote the post that the difference between the two instance variables was that one was outside the define and the other inside. A look at http://www.ruby-doc.org/docs/UsersGuide/rg/instancevars.html and other examples, shows that the instance variable is always defined in a method without saying why. So I was very glad to find your response. I thought of an instance as a copy of the class. so implicitely I assumed it would get a copy of an instance variable. Maybe I should think of an object as an inheritance of methods: So it inherits methods, not variables. But if that was the rule, it would not work for constants either http://www.ruby-doc.org/docs/UsersGuide/rg/constants.html in the example there, it would get a method and have no idea what the constants were. your example helped clarify what is going on. class C puts self @var = 1 puts self def x puts self @var = 2 end end p "create new instance of C" y= C.new p y p "run method which creates an instance variable" y.x p y class D puts self @@var = 1 puts self def x puts self @@var = 2 end end p "create new instance of D" y= D.new p y p "run method which creates an instance variable" y.x p y In class C, running x adds @var to the object, but in D, running x does not add @@var. @var is an instance variable that belongs to whatever object is "self" at that moment in runtime. QUESTION: If that is the case, why does puts self not append @var=1 to object C the way it appends @var=2 to the instance? # In any case, the code in C outside of the methods is only run once when the new method is called, and the object is C. OK. I had not thought of it that way. So is this an ok way to think about objects: 1. Objects inherit methods 2. The variable environment includes, read access to Class constants, r/w to @@var and r/w to global variables 2. @var become local to whatever object is self at the moment they are created.