Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Array.try_convert

[A < ::Array[U], U] (A array) -> A
[U] (::array[U] ary) -> ::Array[U]?
(untyped) -> ::Array[untyped]?
Array.try_convert(object) → object, new_array, or nil

Attempts to return an array, based on the given object.

If object is an array, returns object.

Otherwise if object responds to :to_ary. calls object.to_ary: if the return value is an array or nil, returns that value; if not, raises TypeError.

Otherwise returns nil.

Related: see Methods for Creating an Array.

See also .new #& .[] #* also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0