Vladimir Agafonkin wrote:
> Hi!
>
> What is the best way to access class' instance variables from a method
> of another instance variable of the same class that is a class itself?
> :-)
>
> OK, Let's say I have Duck class with an instance variable
> @quack_behaviour of QuackBehaviour class inside. Duck#quack method
> calls one of the QuackBehaviour methods, and I want to access some of
> the intance variables (say, @name) of the caller Duck object from that
> method.
>
> One way is to set an attr_accessor :name (or use
> instance_variable_get) and pass "self" as a parameter to the
> @quack_behaviour method. But it seems for me that it is not the most
> appropriate way of doing this. Or is it?

Sounds like an application of state or strategy pattern.  I posted some
example code a few days / weeks ago.  Basically, if your two classes are
rather tightly coupled you can have each instance point to the other.
That way both can access methods on the other instance.  The only ugly
thing being that you have to manually keep references in sync.

HTH

    robert