collect with code block : collect « Array « Ruby






collect with code block


p [1, 2, 3, 4].collect { |x| x ** 2 }             # => [1, 4, 9, 16]

 

Related examples in the same category

1.you can convert an array on the fly using the collect method
2.Change array elements with collect!