From: Hassan Schroeder Date: 2011-05-08T08:27:03+09:00 Subject: Re: Cross access between instance variables 2011/5/7 Stefan Salewski : > > Of course this is a way to solve the problem. It may look not very > natural, that we have to tell the fish the parameters of its tank, when > we ask the fish about its mood: > >>mood =  fish.tell_mood({:temp => t.temp, :food => t.food_type}) > > But OK, this is the way to do it. Now I know that I have not missed > something. A little late to this, but isn't this a good case for the Observer pattern? So the fish are always "aware" of the tank environment? Very simply: ----------------------------- require 'observer' class Tank include Observable def temperature @temperature end def temperature=(temperature) @temperature = temperature changed notify_observers(temperature) end end class Fish attr_accessor :tank_temperature def initialize(tank) @tank_temperature = tank.temperature tank.add_observer(self) end def update(temperature) puts "temperature changed to #{temperature}" @tank_temperature = temperature end end ----------------------------- And exercise with: ----------------------------- require 'tank' require 'fish' tank = Tank.new tank.temperature= 100 fish = Fish.new(tank) puts "fish.tank_temperature: #{fish.tank_temperature}" # => 100 tank.temperature= 102 puts "fish.tank_temperature: #{fish.tank_temperature}" # => 102 ----------------------------- FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com twitter: @hassan