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

() -> ::Enumerator[[ K, V ], ::Hash[K, V]]
() { (K key, V value) -> ::boolish } -> ::Hash[K, V]
reject {|key, value| ... } → new_hash
reject → new_enumerator

With a block given, returns a copy of self with zero or more entries removed; calls the block with each key-value pair; excludes the entry in the copy if the block returns a truthy value, includes it otherwise:

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

With no block given, returns a new Enumerator.

Related: see Methods for Deleting.

See also #reject! #rehash #rassoc #replace also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0