Skip to content

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

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.

See also #values #value? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0