Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#to_a

to_a → self or new_array

When self is an instance of Array, returns self:

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

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

Example
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
See also #take_while #to_ary #take #to_h also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0