Skip to content
instance method

Kernel#Hash

[K, V] (nil | [ ] _empty) -> ::Hash[K, V]
[K, V] (::hash[K, V] hash_like) -> ::Hash[K, V]
Hash(object) → object or new_hash

Returns a hash converted from object.

  • If object is:

    • A hash, returns object.

    • An empty array or nil, returns an empty hash.

  • Otherwise, if object.to_hash returns a hash, returns that hash.

  • Otherwise, returns TypeError.

Examples:

Example
Hash({foo: 0, bar: 1}) # => {:foo=>0, :bar=>1}
Hash(nil)              # => {}
Hash([])               # => {}
See also #gsub #Integer #global_variables #iterator? also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0