instance method mattr_accessor

Ruby on Rails edge

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.11.3 v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)

Defines both class and instance accessors for class attributes. 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_accessor :hair_colors

end

class Person

include HairColors

end

HairColors.hair_colors = [:brown, :black, :blonde, :red] HairColors.hair_colors # => [:brown, :black, :blonde, :red] Person.new.hair_colors # => [:brown, :black, :blonde, :red] “‘

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 Citizen < Person end

Citizen.new.hair_colors << :blue Person.new.hair_colors # => [:brown, :black, :blonde, :red, :blue] “‘

To omit the instance writer method, pass ‘instance_writer: false`.

To omit the instance reader method, pass ‘instance_reader: false`.

“‘ module HairColors

mattr_accessor :hair_colors, instance_writer: false, instance_reader: false

end

class Person

include HairColors

end

Person.new.hair_colors = [:brown] # => NoMethodError Person.new.hair_colors # => NoMethodError “‘

Or pass ‘instance_accessor: false`, to omit both instance methods.

“‘ module HairColors

mattr_accessor :hair_colors, instance_accessor: false

end

class Person

include HairColors

end

Person.new.hair_colors = [:brown] # => NoMethodError Person.new.hair_colors # => NoMethodError “‘

You can set a default value for the attribute.

“‘ module HairColors

mattr_accessor :hair_colors, default: [:brown, :black, :blonde, :red]
mattr_accessor(: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

syms rest
instance_reader key = true
instance_writer key = true
instance_accessor key = true
default key = nil
blk block
Source
# File activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 241
  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

Defined in activesupport/lib/active_support/core_ext/module/attribute_accessors.rb line 241 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Module

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close