From 86eaa24570a7a062cf8f08606256e0cd23d2b980 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 12 Jun 2015 08:34:17 +0000 Subject: [PATCH] hash.c: fetch_values * hash.c (rb_hash_fetch_values): add `Hash#fetch_values`. [Feature #10017] [Fix GH-776] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ hash.c | 29 +++++++++++++++++++++++++++++ test/ruby/test_hash.rb | 15 +++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/ChangeLog b/ChangeLog index 2ecc72cb1f..d3fcbfcd32 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Jun 12 17:34:14 2015 Wojciech Mach + + * hash.c (rb_hash_fetch_values): add `Hash#fetch_values`. + [Feature #10017] [Fix GH-776] + Fri Jun 12 16:28:17 2015 Radan Skoric * array.c (rb_ary_bsearch_index): Implement Array#bsearch_index diff --git a/hash.c b/hash.c index 60e980763c..80de813b98 100644 --- a/hash.c +++ b/hash.c @@ -1275,6 +1275,34 @@ rb_hash_values_at(int argc, VALUE *argv, VALUE hash) return result; } +/* + * 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 KeyError when one of keys can't be found. + * Also see Hash#values_at and Hash#fetch. + * + * 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"] + */ + +VALUE +rb_hash_fetch_values(int argc, VALUE *argv, VALUE hash) +{ + VALUE result = rb_ary_new2(argc); + long i; + + for (i=0; i