I’m always on the lookout for beautiful shorthand in Rails and recently I found something lovely in the `Rails#Enumerable` package. If I have a number of records with an attribute (like “amount”, which contains some sort of number) I wish to sum up, I can so this.
payments.sum(&:amount)
This is because the above is shorthand for
payments.inject(0) { |sum, p| sum + p.amount }
Which basically goes over the payments array, takes each element and adds it to `sum`, finally returning whatever this holds. Lovely!