Method: Hash#key

Defined in:
hash.c

#key(value) ⇒ Object

Returns the key for a given value. If not found, returns nil.

h = { "a" => 100, "b" => 200 }
h.key(200)   #=> "b"
h.key(999)   #=> nil


# File 'hash.c'

/*
 *  call-seq:
 *     hsh.key(value)    -> key
 *
 *  Returns the key for a given value. If not found, returns <code>nil</code>.
 *
 *     h = { "a" => 100, "b" => 200 }
 *     h.key(200)   #=> "b"
 *     h.key(999)   #=> nil
 *
 */

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