| David A. Black 2005-08-28, 6:59 pm |
| Hi --
On Fri, 26 Aug 2005, Anatol Pomozov wrote:
> I have several classes that have circular dependencies on each other.
> See following example.
>
> class Solder
s/der/dier/g :-)
> @can_upgrade_to = [Knight]
> #or use constant from Knight class
There's a big difference, though. What you're doing here is assigning
to an instance variable of the class instance Soldier. And what
you're doing here:
> def upgrade!(to)
> raise "Unable to upgrade" unless @can_upgrade_to.contain(to)
> end
> end
is looking up an instance variable of an *instance* of class Soldier.
Simplified example:
class C
@x = 1
def meth
p @x
end
p @x # 1
end
C.new.meth # nil
There are two @x's here. They are both instance variables, but they
belong to different objects. One belongs to the object C (which is an
instance of Class). The other belongs to whatever objects are
instances of C.
Constants are a different thing entirely. If you define a constant in
a class, it's visible throughout. It doesn't belong to the class in
the same private way that a class's instance variables do.
David
--
David A. Black
dblack@wobblini.net
|