Method: Hash#key

Defined in:
hash.c

#key(value) ⇒ nil

Returns the key for the first-found entry with the given value (see Entry Order):

h = {foo: 0, bar: 2, baz: 2}
h.key(0) # => :foo
h.key(2) # => :bar

Returns nil if no such value is found.

Returns:

  • (nil)


2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
# File 'hash.c', line 2337

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];
}