Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Hash#filter

filter

Alias of #select

Returns a new Hash object 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}

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}
See also #select #filter! #fetch_values #fetch also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0