cattr_accessor & attr_accessor
This defines attr_accessors at a class level instead of instance level.
class Foo
cattr_accessor :greeting
end
Foo.greeting = "Hello"
This could be compared to, but is not the same as doing this:
class Bar
class << self
attr_accessor :greeting
end
end
Bar.greeting = "Hello"
The difference might not be apparent at first, but cattr_accessor will make the accessor inherited to the instances:
Foo.new.greeting #=> "Hello"
Bar.new.greeting # NoMethodError: undefined method `greeting' for #
This inheritance is also not copy-on-write in case you assumed that:
Foo.greeting #=> "Hello"
foo1, foo2 = Foo.new, Foo.new
foo1.greeting = "Hi!"
Foo.greeting #=> "Hi!"
foo2.greeting #=> "Hi!"
This makes it possible to share common state (queues, semaphores, etc.), configuration (max value, etc.) or temporary values through this.