Method: Array#to_a

Defined in:
array.c

#to_aself

When self is an instance of Array, returns self:

a = [:foo, 'bar', 2]
a.to_a # => [:foo, "bar", 2]

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

class MyArray < Array; end
a = MyArray.new(['foo', 'bar', 'two'])
a.instance_of?(Array) # => false
a.kind_of?(Array) # => true
a1 = a.to_a
a1 # => ["foo", "bar", "two"]
a1.class # => Array # Not MyArray

Returns:

  • (self)


2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
# File 'array.c', line 2917

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;
}