- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (0)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (29)
- 3.0.9 (26)
- 3.1.0 (0)
- 3.2.1 (-38)
- 3.2.8 (-26)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (-4)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (1)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (-1)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
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