Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

Hash.try_convert

[K, V] (::hash[K, V] hash) -> ::Hash[K, V]
(untyped) -> ::Hash[untyped, untyped]?
Hash.try_convert(object) → object, new_hash, or nil

If object is a hash, returns object.

Otherwise if object responds to :to_hash, calls object.to_hash; returns the result if it is a hash, or raises TypeError if not.

Otherwise if object does not respond to :to_hash, returns nil.

See also .ruby2_keywords_hash? #< .ruby2_keywords_hash #<= also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0