method
collect
v1_8_6_287 -
Show latest stable
- Class:
Enumerable
collect()public
Returns a new array with the results of running block once for every element in enum.
(1..4).collect {|i| i*i } #=> [1, 4, 9, 16] (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]
2Notes
collect_with_index
Use Object#enum_for if you need to collect with index:
require 'enumerator'
%w{foo bar}.enum_for(:each_with_index).collect do |item, index|
"#{index}: #{item}"
end
See also: Enumerable#each_with_index