Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Hash#reject!

hash.reject! {|key, value| ... } → self or nil
hash.reject! → new_enumerator

Returns self, whose remaining entries are those for which the block returns false or nil:

Example
h = {foo: 0, bar: 1, baz: 2}
h.reject! {|key, value| value < 2 } # => {:baz=>2}

Returns nil if no entries are removed.

Returns a new Enumerator if no block given:

Example
h = {foo: 0, bar: 1, baz: 2}
e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
See also #reject #replace #rehash #select also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0