I find this code in Ruby to be pretty intriguing
(1..4).inject(&:+)
Ok, I know what inject
does, and I know this code is basically equivalent to
(1..4).inject(0) {|a,n| a + n}
but how exactly does it work?
Why &:+
is the same as writing the block {|a,n| a + n}
?
Why it doesn't need an initial value? I'm ok with the inicial value being 0, but (1..4).inject(&:*)
also works, and there the initial value must be 1...