class Module
def mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)
end
include HairColors
class Person
end
mattr_accessor :hair_colors, default: [:brown, :black, :blonde, :red]
module HairColors
You can set a default value for the attribute.
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 omit 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 omit the instance reader method, pass instance_reader: false.
To omit the instance writer method, pass instance_writer: false.
Person.new.hair_colors # => [:brown, :black, :blonde, :red, :blue]
Citizen.new.hair_colors << :blue
end
class Citizen < 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
this method is called with a private or protected access modifier.
All class and instance methods created will be public, even if
Defines both class and instance accessors for class attributes.
def mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk) location = caller_locations(1, 1).first mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default, location: location, &blk) mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor, default: default, location: location) end