Skip to content

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

instance method

Hash#rassoc

(::Hash::_Equals value) -> [ K, V ]?
rassoc(value) → new_array or nil

Searches self for the first entry whose value is == to the given value; see Entry Order.

If the entry is found, returns its key and value as a 2-element array; returns nil if not found:

Example
h = {foo: 0, bar: 1, baz: 1}
h.rassoc(1) # => [:bar, 1]

Related: see Methods for Fetching.

See also #merge! #rehash #merge #reject also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0