Ruby Class Variables

You want #cattr_accessor

attr_accessor gives you:

def var; @var; end
def var=(val); @var = val; end

while cattr_accessor gives you:

def self.var; @@var; end
def self.var=(val); @@var = val; end

Which also means that you will normally do Polygon.sides.


attr_accessor writes out set/get for instance variables.

cattr_accessor writes out set/get for class variables.

Use those, and you don’t have to write out your own set/get


Hmm, as you are posting to the Rails mailing list, I assumed the Rails environment. You can include ActiveSupport for yourself to use these methods.

require ‘rubygems’
require ‘active_support’