Guest User

Untitled

a guest
Sep 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.32 KB | None | 0 0
  1. How to sum properties of the objects within an array in Ruby
  2. array = [1,2,3,4,5];
  3. puts array.inject(0, &:+)
  4.  
  5. array.cash.inject(0, &:+) # (but this doesn't work)
  6.  
  7. array.map(&:cash).inject(0, &:+)
  8.  
  9. array.inject(0){|sum,e| sum += a.cash }
  10.  
  11. array.reduce(0) { |sum, obj| sum + obj.cash }
  12.  
  13. array.map(&:cash).inject(:+)
Add Comment
Please, Sign In to add comment