Guys, how are you?
 Watch this simple code first:


class Foo
  @@x = 1
  def self.x
    @@x
  end
  def self.x= (y)
    @@x = y
  end
end

class << Foo
  attr_accessor :x
  @x = 1
end


 I'm developing an aplication, and I'm wondering which way of the both
above I should use, or is recommended to use, I really need your advice,
'cause I'm noob. This is the point: the instances of Foo class almost
will never access the class variable @@x. @@x is just a thing related
with the class itself, which is accessed generally from outside the
class, like...for example:


class Dog
  @@all_the_legs = 0
  def initialize
    @@all_the_legs += 4
  end
end


 And I just want to take a look sometimes at how many legs are...so:


class Dog
  def self.all_the_legs
     @@all_the_legs
  end
end


 This class variable will not be accessed anymore(after the initialize)
by any instance. So I'm wondering if is better to do this instead:


class << Dog
  attr_accessor :all_the_legs
  @all_the_legs = 0
end

class Dog
  def initialize
    Dog.all_the_legs += 4
  end
end


 Because I can see that if I choose the first way, and I want to Inherit
a class from Dog, I will fail:


class Dogo < Dog
  @@all_the_legs = 0 #with and without this line: same result for both
  def initialize
     @@all_the_legs += 4
  end
  def self.all_the_legs
     @@all_the_legs
  end
end

Dog.new; Dog.new; Dog.new; Dog.all_the_legs
#=> 12
Dogo.new; Dogo.new; Dogo.new; Dogo.all_the_legs
#=> 24


  I have examples like this in my whole source code, so I'm thinking
which way to take. Hope you can view my point(the example could not be
the best): I need your advice about if is good to use class variables if
you almost don't use it for the access of instances, instead you need to
require that information from outside the class(suppose that I want to
maintain in a GUI the amount of legs that share all the dogs). It is
good in this cases to enter the singleton class of a class and define
accessors and instance variables to later be accessed? Thank you all.

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