Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 269 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sum array of numbers in Ruby?

#11
Ruby 2.4+ / Rails - `array.sum` i.e. `[1, 2, 3].sum # => 6`

Ruby pre 2.4 - `array.inject(:+)` or `array.reduce(:+)`

*Note: The `#sum` method is a new addition to 2.4 for `enumerable` so you will now be able to use `array.sum` in pure ruby, not just Rails.

Reply

#12

**Method 1:**

[1] pry(main)> [1,2,3,4].sum
=> 10
[2] pry(main)> [].sum
=> 0
[3] pry(main)> [1,2,3,5,nil].sum
TypeError: nil can't be coerced into Integer

**Method 2:**


[24] pry(main)> [].inject(:+)
=> nil
[25] pry(main)> [].inject(0, :+)
=> 0
[4] pry(main)> [1,2,3,4,5].inject(0, :+)
=> 15
[5] pry(main)> [1,2,3,4,nil].inject(0, :+)
TypeError: nil can't be coerced into Integer
from (pry):5:in `+'

**Method 3:**

[6] pry(main)> [1,2,3].reduce(:+)
=> 6
[9] pry(main)> [].reduce(:+)
=> nil
[7] pry(main)> [1,2,nil].reduce(:+)
TypeError: nil can't be coerced into Integer
from (pry):7:in `+'
**Method 4:**
When Array contains an nil and empty values, by default if you use any above functions reduce, sum, inject everything will through the

> TypeError: nil can't be coerced into Integer

You can overcome this by,

[16] pry(main)> sum = 0
=> 0
[17] pry(main)> [1,2,3,4,nil, ''].each{|a| sum+= a.to_i }
=> [1, 2, 3, 4, nil, ""]
[18] pry(main)> sum
=> 10

**Method 6:**
**eval**



Evaluates the Ruby expression(s) in string.


[26] pry(main)> a = [1,3,4,5]
=> [1, 3, 4, 5]
[27] pry(main)> eval a.join '+'
=> 13
[30] pry(main)> a = [1,3,4,5, nil]
=> [1, 3, 4, 5, nil]
[31] pry(main)> eval a.join '+'
SyntaxError: (eval):1: syntax error, unexpected end-of-input
1+3+4+5+
Reply

#13
for array with nil values we can do compact and then inject the sum
ex-

a = [1,2,3,4,5,12,23.45,nil,23,nil]
puts a.compact.inject(:+)
Reply

#14
For Ruby >=2.4.0 you can use `sum` from Enumerables.

[1, 2, 3, 4].sum

It is dangerous to mokeypatch base classes. If you like danger and using an older version of Ruby, you could add `#sum` to the `Array` class:

class Array
def sum
inject(0) { |sum, x| sum + x }
end
end
Reply

#15
If you feel golfy, you can do

eval [123,321,12389]*?+

This will create a string "123+321+12389" and then use function eval to do the sum. This is **only for golfing purpose**, you should not use it in proper code.
Reply

#16
**For ruby >= 2.4** you can use [sum][1]:

`array.sum`

**For ruby < 2.4** you can use [inject][2]:

array.inject(0, :+)

Note: the `0` base case is needed otherwise `nil` will be returned on empty arrays:

> [].inject(:+)
nil
> [].inject(0, :+)
0


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#17
Try this:

array.inject(0){ |sum, x| sum + x }

[See Ruby's Enumerable Documentation][1]


[1]:

[To see links please register here]


(note: the `0` base case is needed so that `0` will be returned on an empty array instead of `nil`)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through