End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Hash#filter!
filter!
hash.c
View on GitHub ↗
static VALUE
rb_hash_select_bang(VALUE hash)
{
st_index_t n;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
n = RHASH_SIZE(hash);
if (!n) return Qnil;
rb_hash_foreach(hash, keep_if_i, hash);
if (n == RHASH_SIZE(hash)) return Qnil;
return hash;
}
Alias of #select!
Hash#filter! is an alias for Hash#select!.
Returns self, whose entries are those for which the block returns a truthy value:
h = {foo: 0, bar: 1, baz: 2}
h.select! {|key, value| value < 2 } => {:foo=>0, :bar=>1}
Returns nil if no entries were removed.
Returns a new Enumerator if no block given:
Example
h = {foo: 0, bar: 1, baz: 2} e = h.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!> e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}