So, I've just learned 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 learned 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 (select) uses the &
and the other one (inject) doesn't. I'm sure that the :
are because even?
and +
are treated at symbols, but I'd love clarification behind why the &
is used in one and why the :
are used.
Also, I'm aware that I could do these notations on more than just inject
and select
.
Many thanks!