Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Struct#select

select {|value| ... } → array
select → enumerator

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.

Struct#filter is an alias for Struct#select.

See also #members #size #length #to_a also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0