Skip to content

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

instance method

Enumerable#collect

enum.collect { |obj| block } → array
enum.collect → an_enumerator

Returns a new array with the results of running block once for every element in enum.

If no block is given, an enumerator is returned instead.

Example
(1..4).map { |i| i*i }      #=> [1, 4, 9, 16]
(1..4).collect { "cat"  }   #=> ["cat", "cat", "cat", "cat"]
See also #chunk_while #collect_concat #chunk #count also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1