Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Module#attr_accessor

attr_accessor(symbol, ...) → array
attr_accessor(string, ...) → array

Defines a named attribute for this module, where the name is symbol.id2name, creating an instance variable (@name) and a corresponding access method to read it. Also creates a method called name= to set the attribute. String arguments are converted to symbols. Returns an array of defined method names as symbols.

Example
module Mod
  attr_accessor(:one, :two) #=> [:one, :one=, :two, :two=]
end
Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]

Ruby version changes

  1. 3.0
See also #attr #attr_reader #append_features #attr_writer also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1