summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-hash-ext/mrblib
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-05-09 11:59:28 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-05-09 11:59:28 +0900
commita9488618ced0001f4f76480f3668ac00c996d446 (patch)
tree9aec19f51070b88dc30542beee972d86be8d2046 /mrbgems/mruby-hash-ext/mrblib
parent0b23d10a965fba7a58404a43c6369e2361388881 (diff)
parentdc4c042df5466287764bf46377ca9baa6690f198 (diff)
downloadmruby-a9488618ced0001f4f76480f3668ac00c996d446.tar.gz
mruby-a9488618ced0001f4f76480f3668ac00c996d446.zip
Merge pull request #2213 from suzukaze/add-hash.key
Add Hash#key
Diffstat (limited to 'mrbgems/mruby-hash-ext/mrblib')
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb20
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