summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2017-10-18 10:05:39 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2017-10-18 10:05:39 +0900
commit44f07045d186b2065103c4095b3a2fe56e3964b5 (patch)
treea5a08789b1eb5df0a0897f236019cda6a5286b26
parent913b83d579487058a2d865b3e430d7e887efea93 (diff)
downloadmruby-44f07045d186b2065103c4095b3a2fe56e3964b5.tar.gz
mruby-44f07045d186b2065103c4095b3a2fe56e3964b5.zip
Add `Hash#fetch_values`; CRuby2.3
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index 73d1fbe6d..7bf06a0ad 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -474,4 +474,25 @@ class Hash
end
self
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