Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between select and collect [closed]

Tags:

ruby

Can't understand the difference between select and collect methods. Also want to know when to use each.

like image 835
Salabh Avatar asked Jun 25 '26 09:06

Salabh


1 Answers

Enumerable#collect (or Enumerable#map) returns a result of applying block to each items.

[1, 2, 3, 4].collect { |x| x > 2 }
# => [false, false, true, true]

While Enumerable#select returns an array of filtered items:

[1, 2, 3, 4].select { |x| x > 2 }
# => [3, 4]
like image 197
falsetru Avatar answered Jun 26 '26 22:06

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!