Skip to content

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

instance method

Array#select

array.select {|element| ... } → new_array
array.select → new_enumerator

Calls the block, if given, with each element of self; returns a new Array containing those elements of self for which the block returns a truthy value:

Example
a = [:foo, 'bar', 2, :bam]
a1 = a.select {|element| element.to_s.start_with?('b') }
a1 # => ["bar", :bam]

Returns a new Enumerator if no block given:

Example
a = [:foo, 'bar', 2, :bam]
a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
See also #select! #sample #rotate! #shelljoin also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0