From: Daniel Schierbeck Date: 2005-11-29T00:57:28+09:00 Subject: Re: Object access heirarchies anne001 wrote: > I am a newbie on ruby, and I am a bit confused by this code, it seems > like a snake eating its own tail! game contains an object world which > is defined as containing game? > > Can someone explain this a little to me? How is that different from > using a @@ class object? > Say you have three classes, A, B, and C, and you want their instances to be able to communicate. These three classes will all be instantiated together, so we know that the other objects are there. You then write a class, let's just call it Base, that you instantiate instead of the three classes. It will insatntiate them instead, and pass a reference to itself to each of them. class Base attr_reader :a, :b, :c def initialize @a, @b, @c = A.new(self), B.new(self), C.new(self) end # We'll keep the other classes in here, so we don't # pollute the main namespace class A def initialize(base) @base = base @b, @c = base.b, base.c end end class B def initialize(base) @base = base @a, @c = base.a, base.c end end class C def initialize(base) @base = base @a, @b = base.a, base.b end end end