Method: Array#to_a

Defined in:
array.c

#to_aself

When self is an instance of Array, returns self.

Otherwise, returns a new array containing the elements of self:

class MyArray < Array; end
my_a = MyArray.new(['foo', 'bar', 'two'])
a = my_a.to_a
a # => ["foo", "bar", "two"]
a.class # => Array # Not MyArray.

Related: see Methods for Converting.

Returns:

  • (self)


3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
# File 'array.c', line 3029

static VALUE
rb_ary_to_a(VALUE ary)
{
    if (rb_obj_class(ary) != rb_cArray) {
        VALUE dup = rb_ary_new2(RARRAY_LEN(ary));
        rb_ary_replace(dup, ary);
        return dup;
    }
    return ary;
}