instance method
Hash#keep_if
() { (K, V) -> ::boolish } -> self
() -> ::Enumerator[[ K, V ], self]
keep_if {|key, value| ... } → self
keep_if → new_enumerator
hash.c
View on GitHub ↗
static VALUE
rb_hash_keep_if(VALUE hash)
{
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
rb_hash_modify_check(hash);
if (!RHASH_TABLE_EMPTY_P(hash)) {
rb_hash_foreach(hash, keep_if_i, hash);
}
return hash;
}
With a block given, calls the block for each key-value pair; retains the entry if the block returns a truthy value; otherwise deletes the entry; returns self:
Example
h = {foo: 0, bar: 1, baz: 2} h.keep_if { |key, value| key.start_with?('b') } # => {bar: 1, baz: 2}
With no block given, returns a new Enumerator.
Related: see Methods for Deleting.