I know that some say that class variables (e.g. @@class_var
) should be avoid in Ruby and should use the an instance variable (e.g. @instance_var
) in the class scope instead:
def MyClass
@@foo = 'bar' # Should not do this.
@foo = 'bar' # Should do this.
end
Why is the use of class variables frowned upon in Ruby?
Class variables are often maligned because of their sometimes confusing behavior regarding inheritance:
If you use class instance variables instead, you get:
This is often more useful.
Be careful; class
@@variables
and instance@variables
are not the same thing.From: http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby