@@ -5355,6 +5355,42 @@ env_fetch(int argc, VALUE *argv, VALUE _)
53555355 return env ;
53565356}
53575357
5358+ /*
5359+ * call-seq:
5360+ * ENV.fetch_values(*names) -> array of values
5361+ * ENV.fetch_values(*names) {|name| ... } -> array of values
5362+ *
5363+ * Returns an Array containing the environment variable values associated with
5364+ * the given names:
5365+ * ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2')
5366+ * ENV.fetch_values('foo', 'baz') # => ["0", "2"]
5367+ *
5368+ * Otherwise if a block is given yields +name+ to
5369+ * the block and returns the block's return value:
5370+ * ENV.fetch_values('foo', 'bam') {|key| key.to_s} # => ["0", "bam"]
5371+ *
5372+ * Raises KeyError if +name+ is valid, but not found and block is not given:
5373+ * ENV.fetch_values('foo', 'bam') # Raises KeyError (key not found: "bam")
5374+ *
5375+ * Returns an empty Array if no names given.
5376+ *
5377+ * Raises an exception if any name is invalid.
5378+ * See {Invalid Names and Values}[rdoc-ref:ENV@Invalid+Names+and+Values].
5379+ */
5380+
5381+ static VALUE
5382+ env_fetch_values (int argc , VALUE * argv , VALUE ehash )
5383+ {
5384+ VALUE result = rb_ary_new2 (argc );
5385+ long i ;
5386+
5387+ for (i = 0 ; i < argc ; i ++ ) {
5388+ rb_ary_push (result , env_fetch (1 , & argv [i ], ehash ));
5389+ }
5390+
5391+ return result ;
5392+ }
5393+
53585394#if defined(_WIN32 ) || (defined(HAVE_SETENV ) && defined(HAVE_UNSETENV ))
53595395#elif defined __sun
53605396static int
@@ -7577,6 +7613,7 @@ Init_Hash(void)
75777613 * - ::clone: Raises an exception.
75787614 * - ::except: Returns a hash of all name/value pairs except those given.
75797615 * - ::fetch: Returns the value for the given name.
7616+ * - ::fetch_values: Returns array containing the values associated with given names.
75807617 * - ::inspect: Returns the contents of +ENV+ as a string.
75817618 * - ::invert: Returns a hash whose keys are the +ENV+ values,
75827619 and whose values are the corresponding +ENV+ names.
@@ -7612,6 +7649,7 @@ Init_Hash(void)
76127649
76137650 rb_define_singleton_method (envtbl , "[]" , rb_f_getenv , 1 );
76147651 rb_define_singleton_method (envtbl , "fetch" , env_fetch , -1 );
7652+ rb_define_singleton_method (envtbl , "fetch_values" , env_fetch_values , -1 );
76157653 rb_define_singleton_method (envtbl , "[]=" , env_aset_m , 2 );
76167654 rb_define_singleton_method (envtbl , "store" , env_aset_m , 2 );
76177655 rb_define_singleton_method (envtbl , "each" , env_each_pair , 0 );
0 commit comments