Flowdock

Extends the class object with class and instance accessors for class attributes, just like the native attr* accessors for instance attributes.

Note that unlike class_attribute, if a subclass changes the value then that would also change the value for parent class. Similarly if parent class changes the value then that would change the value of subclasses too.

class Person
  cattr_accessor :hair_colors
end

Person.hair_colors = [:brown, :black, :blonde, :red]
Person.hair_colors     # => [:brown, :black, :blonde, :red]
Person.new.hair_colors # => [:brown, :black, :blonde, :red]

To opt out of the instance writer method, pass :instance_writer => false. To opt out of the instance reader method, pass :instance_reader => false. To opt out of both instance methods, pass :instance_accessor => false.

class Person
  cattr_accessor :hair_colors, :instance_writer => false, :instance_reader => false
end

Person.new.hair_colors = [:brown]  # => NoMethodError
Person.new.hair_colors             # => NoMethodError
Show files where this class is defined (5 files)
Register or log in to add new notes.