method
cattr_writer
v3.2.13 -
Show latest stable
- Class:
Class
cattr_writer(*syms)public
Defines a class attribute if it’s not defined and creates a writer method to allow assignment to the attribute.
class Person cattr_writer :hair_colors end Person.hair_colors = [:brown, :black] Person.class_variable_get("@@hair_colors") # => [:brown, :black] Person.new.hair_colors = [:blonde, :red] Person.class_variable_get("@@hair_colors") # => [:blonde, :red]
The attribute name must be a valid method name in Ruby.
class Person cattr_writer :"1_Badname " end # => NameError: invalid attribute name
If you want to opt out the instance writer method, pass :instance_writer => false or :instance_accessor => false.
class Person cattr_writer :hair_colors, :instance_writer => false end Person.new.hair_colors = [:blonde, :red] # => NoMethodError
Also, you can pass a block to set up the attribute with a default value.
class Person cattr_writer :hair_colors do [:brown, :black, :blonde, :red] end end Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]