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

(*::Hash::_Key keys) -> ::Hash[K, V]
except(*keys) → new_hash

Returns a copy of self that excludes entries for the given keys; any keys that are not found are ignored:

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

Related: see Methods for Deleting.

Ruby version changes

  1. 3.0
See also #eql? #fetch #empty? #fetch_values also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0