Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 41
    Please note that Ruby 2.4+ has array.sum Commented Jul 3, 2017 at 18:58
  • 1
    Ruby 2.6 does not have it. Ruby giveth, Ruby taketh away, it seems. Commented Jan 22, 2019 at 20:38
  • 2
    @Lori hmm ? link Commented Jun 26, 2019 at 15:22
  • 2
    Sorry. At that time I mistakenly believed I was using 2.6 because of a rbenv slip-up on my part. Commented Jun 27, 2019 at 0:15
  • If you need to supply a default value for when the Array is empty, like if you want to return a Money object instead of an Integer, you can do something like array.sum( 0.to_money( "USD" ) ). Commented Mar 5, 2021 at 16:05