Hi –
On Mon, 10 Jul 2006, anne001 wrote:
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.
“Inheritance” is a bit of a misnomer. A class (or module) contains
methods. Every object has a method look-up path, which traverses
multiple classes and modules. When you call a method, the object
searches for a matching method in the relevant classes and modules, in
a prescribed order.
That sounds like a wordy way to put it – but when you start getting
into things like including modules in singleton classes, and what not,
the look-up path view is the one that will keep it straight for you.
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.
Constants have different scope, and they always belong to a class or
module.
end
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”
Actually you’re using class variables here (@@var). They’re very
different from instance variables.
object C the way it
appends @var=2 to the instance?
#<C:0x25b58 @var=2>
That’s just the convention of the string representation of classes vs.
non-classes.
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.
Actually the code in C outside of the methods is run when the
interpreter first encounters it. Even if you never called C.new, the
code in the definition block of C would be executed.
So is this an ok way to think about objects:
- Objects inherit methods
See above.
- The variable environment includes, read access to Class constants,
r/w to @@var and r/w to global variables
@@var (class variables) have some strange scoping rules, but basically
inside an instance method and outside the method, in the class body,
the @@var you see is the same @@var (unlike instance variables).
-
@var become local to whatever object is self at the moment they are
created.
And whenever you see @var, it’s the @var belonging to self.
David