Hi,

You should definitely use class instance variables. The only situation 
where you might use a class variable is when you actually want the 
variable to be shared by the whole class family (i. e. all subclasses). 
Otherwise it makes no sense and can easily lead to problems (which you 
already discovered). Do *not* use it just to share the variable between 
the class and the instances. That's not what it's meant for. Many people 
even avoid class variables completely, because they're so hard to 
control.

By the way, your code does not do what you think it does. When you set a 
class instance variable in the singleton class, then this variable 
actually belongs to the singleton class and not the original class 
(which is the whole point of instance variables). So you have to set the 
variable in the Foo/Dog class:

class Foo
  @x = 0
  class << self
    attr_accessor :x
  end
end

or shorter

class Foo
  @x = 0
  singleton_class.send :attr_accessor, :x
end

-- 
Posted via http://www.ruby-forum.com/.