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