class Module
def mattr_accessor(*syms, &blk)
end
include HairColors
class Person
end
end
[:brown, :black, :blonde, :red]
mattr_accessor :hair_colors do
module HairColors
Also you can pass a block to set up the attribute with a default value.
Person.new.hair_colors # => NoMethodError
Person.new.hair_colors = [:brown] # => NoMethodError
end
include HairColors
class Person
end
mattr_accessor :hair_colors, instance_accessor: false
module HairColors
Or pass instance_accessor: false, to opt out both instance methods.
Person.new.hair_colors # => NoMethodError
Person.new.hair_colors = [:brown] # => NoMethodError
end
include HairColors
class Person
end
mattr_accessor :hair_colors, instance_writer: false, instance_reader: false
module HairColors
To opt out of the instance reader method, pass instance_reader: false.
To opt out of the instance writer method, pass instance_writer: false.
Person.new.hair_colors # => [:brown, :black, :blonde, :red, :blue]
Male.new.hair_colors << :blue
end
class Male < Person
change the value of subclasses too.
parent class. Similarly if parent class changes the value then that would
If a subclass changes the value then that would also change the value for
Person.new.hair_colors # => [:brown, :black, :blonde, :red]
HairColors.hair_colors # => [:brown, :black, :blonde, :red]
HairColors.hair_colors = [:brown, :black, :blonde, :red]
end
include HairColors
class Person
end
mattr_accessor :hair_colors
module HairColors
Defines both class and instance accessors for class attributes.
def mattr_accessor(*syms, &blk) mattr_reader(*syms, &blk) mattr_writer(*syms) end