summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-hash-ext/mrblib/hash.rb
diff options
context:
space:
mode:
authorYAMAMOTO Masaya <[email protected]>2017-11-04 01:23:12 +0900
committerYAMAMOTO Masaya <[email protected]>2017-11-04 01:23:12 +0900
commit625f9f6fa314872968632c5adbee7fb3823268b8 (patch)
treefdde1700b13048212606e4a995907f3757e18e2f /mrbgems/mruby-hash-ext/mrblib/hash.rb
parentb70d69de09130ce2bc89289b4826b3deea8afaae (diff)
parente7fe6ee2638dee438c1d79ab16a0403aebec0a60 (diff)
downloadmruby-625f9f6fa314872968632c5adbee7fb3823268b8.tar.gz
mruby-625f9f6fa314872968632c5adbee7fb3823268b8.zip
Merge branch 'master' of github.com:mruby/mruby
Diffstat (limited to 'mrbgems/mruby-hash-ext/mrblib/hash.rb')
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index 73d1fbe6d..d1a709325 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -474,4 +474,29 @@ class Hash
end
self
end
+
+ def to_proc
+ ->x{self[x]}
+ end
+
+ ##
+ # call-seq:
+ # hsh.fetch_values(key, ...) -> array
+ # hsh.fetch_values(key, ...) { |key| block } -> array
+ #
+ # Returns an array containing the values associated with the given keys
+ # but also raises <code>KeyError</code> when one of keys can't be found.
+ # Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>.
+ #
+ # h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
+ #
+ # h.fetch_values("cow", "cat") #=> ["bovine", "feline"]
+ # h.fetch_values("cow", "bird") # raises KeyError
+ # h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
+ #
+ def fetch_values(*keys, &block)
+ keys.map do |k|
+ self.fetch(k, &block)
+ end
+ end
end