Skip to content
class method

Array.try_convert

[U] (untyped) -> ::Array[U]?
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.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0