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#slice

[K2 < ::Hash::_Key] (*K2 keys) -> ::Hash[K2, V]
slice(*keys) → new_hash

Returns a new hash containing the entries from self for the given keys; ignores any keys that are not found:

Example
h = {foo: 0, bar: 1, baz: 2}
h.slice(:baz, :foo, :nosuch) # => {baz: 2, foo: 0}

Related: see Methods for Deleting.

See also #size #store #shift #to_a also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0