Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to sum properties of the objects within an array in Ruby
- array = [1,2,3,4,5];
- puts array.inject(0, &:+)
- array.cash.inject(0, &:+) # (but this doesn't work)
- array.map(&:cash).inject(0, &:+)
- array.inject(0){|sum,e| sum += a.cash }
- array.reduce(0) { |sum, obj| sum + obj.cash }
- array.map(&:cash).inject(:+)
Add Comment
Please, Sign In to add comment