Skip to content

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

instance method

Hash#assoc

hash.assoc(key) → new_array or nil

If the given key is found, returns a 2-element Array containing that key and its value:

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

Returns nil if key key is not found.

See also #any? #clear #[]= #compact also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0