What is the difference between cattr_accessor and attr_accessor? First off, cattr_accessor is part of Ruby on Rails ActiveSupport and not a feature of the Ruby language like attr_accessor.
cattr_accessor is class level equivalent of attr_accessor and can be classified as a singleton method. So cattr_accessor operates at the class level, while attr_accessor operates at the instance level.
Here is an example using a Counter class:
class Counter cattr_accessor :class_count attr_accessor :instance_count end counter1 = Counter.new counter1.instance_count = 1 counter1.class_count = 1 counter2 = Counter.new p counter2.instance_count #> nil p counter2.class_count #> 1
As you can see the cattr_accessor stays the same for every instance, while the attr_accessor is only on the instance of the class.
Behind the scenes it is just using class and instance variables for the getter/setter methods.