# How to Find the Sum of Array of Numbers in Ruby?

In Ruby 2.4+, you can calculate the sum of all numbers in an array using the `Array#sum` method, for example, in the following way:

```# Ruby 2.4+
print [1, 2, 3, 4, 5].sum #=> 15
print [1.2, -4, 4.5, 3, 10].sum #=> 14.7
print [].sum #=> 0
```

It will throw a `TypeError` for non-numeric values (including numeric strings):

```# Ruby 2.4+
# String can't be coerced into Integer (TypeError)
print ['foo', 1, 2, 3].sum

# String can't be coerced into Integer (TypeError)
print ['1', '2', '3', '4', '5'].sum

# nil can't be coerced into Integer (TypeError)
print [1, 2, 3, nil].sum

# ...
```

For versions of Ruby earlier than 2.4, you can use the `Enumerable#inject` method (or its alias `Enumerable#reduce`) instead, in the following way:

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

Passing `0` as the initial operand (or base case) is important as `nil` would be returned for empty arrays otherwise.

You may shorten this using the following syntax:

```array.inject(0, :+)
```

This follows the following method signature for `Enumerable#inject` (where `+` is passed as a symbol to the method):

```# inject(initial, symbol) → obj
```

For example:

```def sum(numbers)
numbers.inject(0, :+)
end

print sum([1, 2, 3, 4, 5]) #=> 15
print sum([1.2, -4, 4.5, 3, 10]) #=> 14.7
print sum([]) #=> 0
```

It will throw a `TypeError` for non-numeric values (including numeric strings):

```# String can't be coerced into Integer (TypeError)
print sum(['foo', 1, 2, 3])

# String can't be coerced into Integer (TypeError)
print sum(['1', '2', '3', '4', '5'])

# nil can't be coerced into Integer (TypeError)
print sum([1, 2, 3, nil])

# ...
```

Hope you found this post useful. It was published . Please show your love and support by sharing this post.
{"ft_lang_label":"__label__en","ft_lang_prob":0.79384536,"math_prob":0.87146527,"size":1664,"snap":"2023-40-2023-50","text_gpt3_token_len":515,"char_repetition_ratio":0.12710844,"word_repetition_ratio":0.17605634,"special_character_ratio":0.35396636,"punctuation_ratio":0.22074468,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98994356,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-23T11:34:05Z\",\"WARC-Record-ID\":\"<urn:uuid:8c72d83c-03de-471d-abc8-31a40e7351dc>\",\"Content-Length\":\"502068\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ba52cd44-4b48-4f07-b999-37c5aae49004>\",\"WARC-Concurrent-To\":\"<urn:uuid:6bf76b66-c5e0-4339-9cf0-748180710336>\",\"WARC-IP-Address\":\"104.21.56.31\",\"WARC-Target-URI\":\"https://www.designcise.com/web/tutorial/how-to-find-the-sum-of-array-of-numbers-in-ruby\",\"WARC-Payload-Digest\":\"sha1:AKQLOKWMV7XXAUJCLNX55YAWT3FH7EUL\",\"WARC-Block-Digest\":\"sha1:AYHOFUR2ENUAYP6YR72G7VAWNQYBO7PI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506480.7_warc_CC-MAIN-20230923094750-20230923124750-00391.warc.gz\"}"}