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

() -> ::Enumerator[[ K, V ], ::Hash[K, V]]
() { (K key, V value) -> ::boolish } -> ::Hash[K, V]
filter

Alias of #select

With a block given, calls the block with each entry’s key and value; returns a new hash whose entries are those for which the block returns a truthy value:

Example
h = {foo: 0, bar: 1, baz: 2}
h.select {|key, value| value < 2 } # => {foo: 0, bar: 1}

With no block given, returns a new Enumerator.

Related: see Methods for Deleting.

See also #select #filter! #fetch_values #fetch also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0