mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Add Hash#fetch_values; CRuby2.3
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user