Method: Module#attr_accessor

Defined in:
object.c

#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.

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

Overloads:

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

    Returns:

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

    Returns:

[View source]

2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
# File 'object.c', line 2357

static VALUE
rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
{
    int i;
    VALUE names = rb_ary_new2(argc * 2);

    for (i=0; i<argc; i++) {
	ID id = id_for_attr(klass, argv[i]);

	rb_attr(klass, id, TRUE, TRUE, TRUE);
	rb_ary_push(names, ID2SYM(id));
	rb_ary_push(names, ID2SYM(rb_id_attrset(id)));
    }
    return names;
}