Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4006

Cross access between instance variables

From Stefan Salewski <mail@ssalewski.de>
Newsgroups comp.lang.ruby
Subject Cross access between instance variables
Date 2011-05-05 14:06 -0500
Organization Service de news de lacave.net
Message-ID <1304622204.2610.22.camel@AMD64X2.fritz.box> (permalink)

Show all headers | View raw


I am working on a larger Ruby program, where instance variables of one
class need access to content of another class -- I wonder how I should
solve this problem best.

Assume we have multiple fish tanks, each with multiple fishes. The
fishes should tell us if they like the temperature of the water. We can
solve this task when we gave each fish a reference to its tank:

stefan@AMD64X2 ~/pet $ cat fish_tank.rb 
class Fish
  def initialize(name, favorite_temperature, tank)
    @tank = tank
    @n = name
    @t = favorite_temperature
  end
  def tell_state
    if @tank.temperature < @t
      puts 'I feel cold'
    elsif @tank.temperature = @t
      puts 'I feel well'
    else
      puts 'I feel hot'
    end
  end
end

class Tank
  attr_accessor :fish
  attr_reader :temperature
  def initialize(temperature)
    @temperature = temperature
    @fish = Array.new
  end
end

t1 = Tank.new(18)
f1 = Fish.new('Ruby', 21, t1)
t1.fish << f1
t1.fish.each{|f| f.tell_state}

I am more familiar with a situation, where the tank is a module, so the
fish can access module variables. But now we really need many tanks,
each with very many fishes. Currently it seems to be a bit strange
giving each of thousands fishes a reference to its tank. Another
solution may be to give the .tell_state() function a tank parameter each
time when we call it. (A tank-temperature parameter is not a good idea,
because we may need more, light, food...) 

(For my real application the tanks are windows, and fishes are graphical
elements in each window, see
http://www.ssalewski.de/PetEd-Demo.html.en) 

Do I miss something?

Best regards,

Stefan Salewski


Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Cross access between instance variables Stefan Salewski <mail@ssalewski.de> - 2011-05-05 14:06 -0500
  Re: Cross access between instance variables Josh Cheek <josh.cheek@gmail.com> - 2011-05-05 14:47 -0500
  Re: Cross access between instance variables Johannes Held <johannes.held@informatik.uni-erlangen.de> - 2011-05-06 10:23 +0200
    Re: Cross access between instance variables Stefan Salewski <mail@ssalewski.de> - 2011-05-07 16:27 -0500
      Re: Cross access between instance variables Hassan Schroeder <hassan.schroeder@gmail.com> - 2011-05-07 18:27 -0500
        Re: Cross access between instance variables John Feminella <johnf@bitsbuilder.com> - 2011-05-07 18:36 -0500
          Re: Cross access between instance variables Hassan Schroeder <hassan.schroeder@gmail.com> - 2011-05-07 19:31 -0500
            Re: Cross access between instance variables John Feminella <johnf@bitsbuilder.com> - 2011-05-07 19:51 -0500

csiph-web