class Class
Ruby on Rails 3.1.12
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.
class Person
cattr_accessor :hair_colors, :instance_writer => false, :instance_reader => false
end
Person.new.hair_colors = [:brown] # => NoMethodError
Person.new.hair_colors # => NoMethodError
Inherits from
Methods (defined here)
Private methods (3)
Show private methods Implementation detail — not part of the public API.
Methods (inherited)
From Object
(24)
- # acts_like?
- # app
- # blank?
- # controller
- # create_fixtures
- # duplicable?
- # helper
- # html_safe?
- # in?
- # index
- # instance_variable_names
- # new_session
- # options
- # presence
- # present?
- # reload!
- # show
- # test_homepage
- # to_param
- # to_query
- # try
- # unescape
- # with_options
- self. table_name_prefix