Skip to content

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

instance method

Enumerable#select

() { (E) -> ::boolish } -> ::Array[E]
() -> ::Enumerator[E, ::Array[E]]
select {|element| ... } → array
select → enumerator

Alias of #find_all

Returns an array containing elements selected by the block.

With a block given, calls the block with successive elements; returns an array of those elements for which the block returns a truthy value:

Example
(0..9).select {|element| element % 3 == 0 } # => [0, 3, 6, 9]
a = {foo: 0, bar: 1, baz: 2}.select {|key, value| key.start_with?('b') }
a # => {:bar=>1, :baz=>2}

With no block given, returns an Enumerator.

Related: reject.

See also #find_all #reverse_each #slice_after #reject also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0