Method: Module#attr_accessor

Defined in:
object.c

#attr_accessor(symbol, ...) ⇒ nil (private) #attr_accessor(string, ...) ⇒ nil (private)

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.

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

Overloads:

  • #attr_accessor(symbol, ...) ⇒ nil

    Returns:

    • (nil)
  • #attr_accessor(string, ...) ⇒ nil

    Returns:

    • (nil)


2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
# File 'object.c', line 2039

static VALUE
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
{
    int i;

    for (i=0; i<argc; i++) {
	rb_attr(klass, id_for_attr(argv[i]), TRUE, TRUE, TRUE);
    }
    return Qnil;
}