is there an easy way to add all thevalues stored in an array together?
i tried total = @array.total but i dont think thats right?
thanks for any help
is there an easy way to add all thevalues stored in an array together?
i tried total = @array.total but i dont think thats right?
thanks for any help
total = @array.inject(0) { |sum, value| sum + value }
The inject method comes from the Enumerable module, which Array
includes.
Docs are here: module Enumerable - RDoc Documentation
.
Regards,
Craig
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs