diff options
| author | Jun Hiroe <[email protected]> | 2014-05-08 17:53:41 +0900 |
|---|---|---|
| committer | Jun Hiroe <[email protected]> | 2014-05-09 00:46:33 +0900 |
| commit | dc4c042df5466287764bf46377ca9baa6690f198 (patch) | |
| tree | 42a0e5d8917a36cb5583602edb1d278e83546f47 /mrbgems/mruby-hash-ext/mrblib | |
| parent | c071ad13a9df20e8e030320eef0a483bf4e64932 (diff) | |
| download | mruby-dc4c042df5466287764bf46377ca9baa6690f198.tar.gz mruby-dc4c042df5466287764bf46377ca9baa6690f198.zip | |
Add Hash#key
Diffstat (limited to 'mrbgems/mruby-hash-ext/mrblib')
| -rw-r--r-- | mrbgems/mruby-hash-ext/mrblib/hash.rb | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index a5f04e5e1..259b0fa12 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -160,4 +160,24 @@ class Hash end self end + + ## + # call-seq: + # hsh.key(value) -> key + # + # Returns the key of an occurrence of a given value. If the value is + # not found, returns <code>nil</code>. + # + # h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 } + # h.key(200) #=> "b" + # h.key(300) #=> "c" + # h.key(999) #=> nil + # + + def key(val) + self.each do |k, v| + return k if v == val + end + nil + end end |
