class Array

def self.wrap(object)

Experimental RBS support (using type sampling data from the type_fusion project).

type Array_wrap_object = UserHomeLocation | Array[Array, String] | Array[String] | Array[Array, String, String] | Array[ClassDefinition] | Array[] | Array[Array, String, String, String] | Array[Array, String, String, String, String] | Array[UserTrade] | Symbol | String
type Array_wrap_return_value = Array[Array, String] | Array[] | Array[String] | Array[Array, String, String]

def self.wrap: (Array_wrap_object object) -> Array_wrap_return_value

This signature was generated using 5776 samples from 2 applications.

apply to the rest of objects.
The differences with Kernel#Array explained above

which returns [] for +nil+, but calls to Array(object) otherwise.

[*object]

There's also a related idiom that uses the splat operator:

Array.wrap(foo: :bar) # => [{:foo=>:bar}]
Array(foo: :bar) # => [[:foo, :bar]]

The last point is easily explained with some enumerables:

it returns an array with the argument as its single element.
* It does not call +to_a+ on the argument, if the argument does not respond to +to_ary+
raises an exception, while Array.wrap does not, it just returns the value.
* If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, Kernel#Array
an array with the argument as its single element right away.
moves on to try +to_a+ if the returned value is +nil+, but Array.wrap returns
* If the argument responds to +to_ary+ the method is invoked. Kernel#Array

This method is similar in purpose to Kernel#Array, but there are some differences:

Array.wrap(0) # => [0]
Array.wrap([1, 2, 3]) # => [1, 2, 3]
Array.wrap(nil) # => []

* Otherwise, returns an array with the argument as its single element.
* Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
* If the argument is +nil+ an empty array is returned.

Specifically:

Wraps its argument in an array unless it is already an array (or array-like).
def self.wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end