Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Hash#reject
() -> ::Enumerator[[ K, V ], ::Hash[K, V]]
() { (K, V) -> ::boolish } -> ::Hash[K, V]
hash.reject {|key, value| ... } → new_hash
hash.reject → new_enumerator
hash.c
View on GitHub ↗
static VALUE
rb_hash_reject(VALUE hash)
{
VALUE result;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
result = hash_dup_with_compare_by_id(hash);
if (!RHASH_EMPTY_P(hash)) {
rb_hash_foreach(result, delete_if_i, result);
compact_after_delete(result);
}
return result;
}
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}