Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Hash#values_at
(*::Hash::_Key arg0) -> ::Array[V?]
values_at(*keys) → new_array
hash.c
View on GitHub ↗
static VALUE
rb_hash_values_at(int argc, VALUE *argv, VALUE hash)
{
VALUE result = rb_ary_new2(argc);
long i;
for (i=0; i<argc; i++) {
rb_ary_push(result, rb_hash_aref(hash, argv[i]));
}
return result;
}
Returns a new array containing values for the given keys:
Example
h = {foo: 0, bar: 1, baz: 2} h.values_at(:baz, :foo) # => [2, 0]
The hash default is returned for each key that is not found:
Example
h.values_at(:hello, :foo) # => [nil, 0]
Related: see Methods for Fetching.