Source From Here
Question
I have an array of integers. For example:
Is there any nice way to get the sum of them? An ugly way is:
How-To
Array in Ruby include Enumerable module. So you can try inject method:
For example:
Question
I have an array of integers. For example:
- array = [123,321,12389]
- sum = 0
- array.each { |a| sum+=a }
Array in Ruby include Enumerable module. So you can try inject method:
For example:
沒有留言:
張貼留言