instance method
mattr_writer
Ruby on Rails 8.0.4
Since v2.2.3Signature
mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil, location: nil)
Defines a class attribute and creates a class and instance writer methods to allow assignment to the attribute. All class and instance methods created will be public, even if this method is called with a private or protected access modifier.
module HairColors mattr_writer :hair_colors end class Person include HairColors end HairColors.hair_colors = [:brown, :black] Person.class_variable_get("@@hair_colors") # => [:brown, :black] Person.new.hair_colors = [:blonde, :red] HairColors.class_variable_get("@@hair_colors") # => [:blonde, :red]
To omit the instance writer method, pass instance_writer: false or instance_accessor: false.
module HairColors mattr_writer :hair_colors, instance_writer: false end class Person include HairColors end Person.new.hair_colors = [:blonde, :red] # => NoMethodError
You can set a default value for the attribute.
module HairColors mattr_writer :hair_colors, default: [:brown, :black, :blonde, :red] mattr_writer(:hair_styles) { [:long, :short] } end class Person include HairColors end Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red] Person.class_variable_get("@@hair_styles") # => [:long, :short]
Parameters
-
symsrest -
instance_writerkey = true -
instance_accessorkey = true -
defaultkey = nil -
locationkey = nil
Source
# File activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 121
def mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil, location: nil)
raise TypeError, "module attributes should be defined directly on class, not singleton" if singleton_class?
location ||= caller_locations(1, 1).first
definition = []
syms.each do |sym|
raise NameError.new("invalid attribute name: #{sym}") unless /\A[_A-Za-z]\w*\z/.match?(sym)
definition << "def self.#{sym}=(val); @@#{sym} = val; end"
if instance_writer && instance_accessor
definition << "def #{sym}=(val); @@#{sym} = val; end"
end
sym_default_value = (block_given? && default.nil?) ? yield : default
class_variable_set("@@#{sym}", sym_default_value) unless sym_default_value.nil? && class_variable_defined?("@@#{sym}")
end
module_eval(definition.join(";"), location.path, location.lineno)
end
Defined in activesupport/lib/active_support/core_ext/module/attribute_accessors.rb line 121
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in Module