diff --git a/hash.c b/hash.c index fc8bfa24eb2f36..c09c7b26b5414a 100644 --- a/hash.c +++ b/hash.c @@ -5355,6 +5355,42 @@ env_fetch(int argc, VALUE *argv, VALUE _) return env; } +/* + * call-seq: + * ENV.fetch_values(*names) -> array of values + * ENV.fetch_values(*names) {|name| ... } -> array of values + * + * Returns an Array containing the environment variable values associated with + * the given names: + * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') + * ENV.fetch_values('foo', 'baz') # => ["0", "2"] + * + * Otherwise if a block is given yields +name+ to + * the block and returns the block's return value: + * ENV.fetch_values('foo', 'bam') {|key| key.to_s} # => ["0", "bam"] + * + * Raises KeyError if +name+ is valid, but not found and block is not given: + * ENV.fetch_values('foo', 'bam') # Raises KeyError (key not found: "bam") + * + * Returns an empty Array if no names given. + * + * Raises an exception if any name is invalid. + * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values]. + */ + +static VALUE +env_fetch_values(int argc, VALUE *argv, VALUE ehash) +{ + VALUE result = rb_ary_new2(argc); + long i; + + for (i=0; i { + ENV.fetch_values("bar", "y", "foo", "z") + }.should raise_error(KeyError, 'key not found: "y"') + end + + it "uses the locale encoding" do + ENV.fetch_values(ENV.keys.first).first.encoding.should == ENVSpecs.encoding + end + + it "raises TypeError when a key is not coercible to String" do + ENV["foo"] = "oof" + -> { ENV.fetch_values("foo", Object.new) }.should raise_error(TypeError, "no implicit conversion of Object into String") + end + end +end