Skip to content
instance method

Hash#keep_if

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

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.

See also #invert #key #inspect #key? also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0