Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Struct#filter

() -> ::Enumerator[E, ::Array[E]]
() { (E value) -> ::boolish } -> ::Array[E]
filter(*args)

Alias of #select

With a block given, returns an array of values from self for which the block returns a truthy value:

Example
Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
a = joe.select {|value| value.is_a?(String) }
a # => ["Joe Smith", "123 Maple, Anytown NC"]
a = joe.select {|value| value.is_a?(Integer) }
a # => [12345]

With no block given, returns an Enumerator.

See also #select #eql? #hash #each_pair also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0