Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Hash#key
(V) -> K?
hash.key(value) → key or nil
hash.c
View on GitHub ↗
static VALUE
rb_hash_key(VALUE hash, VALUE value)
{
VALUE args[2];
args[0] = value;
args[1] = Qnil;
rb_hash_foreach(hash, key_i, (VALUE)args);
return args[1];
}
Returns the key for the first-found entry with the given value (see Entry Order):
Example
h = {foo: 0, bar: 2, baz: 2} h.key(0) # => :foo h.key(2) # => :bar
Returns nil if no such value is found.