Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

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

Was this page useful?