Skip to content

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

instance method

Kernel#Array

Array(object) → object or new_array

Returns an array converted from object.

Tries to convert object to an array using to_ary first and to_a second:

Example
Array([0, 1, 2])        # => [0, 1, 2]
Array({foo: 0, bar: 1}) # => [[:foo, 0], [:bar, 1]]
Array(0..4)             # => [0, 1, 2, 3, 4]

Returns object in an array, [object], if object cannot be converted:

Example
Array(:foo)             # => [:foo]
See also #abort #at_exit #` #autoload also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0