Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Hash#assoc

(K arg0) -> [ K, V ]?
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.3 · 3.2 · 3.1 · 3.0