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
array.c
View on GitHub ↗
static VALUE
rb_ary_s_try_convert(VALUE dummy, VALUE ary)
{
return rb_check_array_type(ary);
}
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.