On 24/01/06, Vladimir Agafonkin <agafonkin / gmail.com> wrote:
> 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?

Huh?

  class QuackBehaviour
    def loudly
	  "LOUDLY"
	end
  end

  class Duck
    def initialize
	  @quack_behaviour = QuackBehaviour.new
	  @name = "Daffy"
	end

    def quack
	  @quack_behaviour.loudly
	end
  end

There's nothing in the above that even remotely allows for
QuackBehaviour#loudly to reach inside of the Duck instance. You will, as
you said, need to either register the duck instance or pass +self+ as a
parameter.

-austin
--
Austin Ziegler * halostatue / gmail.com
               * Alternate: austin / halostatue.ca