Skip to content

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

instance method

Hash#reject

hash.reject {|key, value| ... } → new_hash
hash.reject → new_enumerator

Returns a new Hash object whose entries are all those from self for which the block returns false or nil:

Example
h = {foo: 0, bar: 1, baz: 2}
h1 = h.reject {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}

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>
h1 = e.each {|key, value| key.start_with?('b') }
h1 # => {:foo=>0}
See also #reject! #rehash #rassoc #replace also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0