Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#Hash

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 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0