So, I just found out that instead of writing things like:
[1,2,3,4,5].inject {|x,y| x + y} => 15
I could write
[1,2,3,4,5].inject(:+) => 15
I also found out that instead of writing
[1,2,3,4,5].select {|x| x.even?} => [2,4]
I could write
[1,2,3,4,5].select(&:even?) => [2,4]
My question is why one (chooses) uses & , and the other (injection) doesn't. I am sure that : due to the fact that even? and + processed by characters, but I would like to explain why & used in one and why it is used :
In addition, I know that I could make these not only inject and select notations.
Many thanks!
David
source share