diff --git a/hash.c b/hash.c index 4bf0ae3..d30828c 100644 --- a/hash.c +++ b/hash.c @@ -1202,6 +1202,34 @@ rb_hash_values_at(int argc, VALUE *argv, VALUE hash) return result; } +/* + * call-seq: + * hsh.fetch_at(key, ...) -> array + * hsh.fetch_at(key, ...) { |key| block } -> array + * + * Return an array containing the values associated with the given keys + * but also raises an 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_at("cow", "cat") #=> ["bovine", "feline"] + * h.fetch_at("cow", "bird") # raises KeyError + * h.fetch_at("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"] + */ + +VALUE +rb_hash_fetch_at(int argc, VALUE *argv, VALUE hash) +{ + VALUE result = rb_ary_new2(argc); + long i; + + for (i=0; i