# ActiveSupport Enumerable#sum should not use #size

**URL:** <https://www.ruby-forum.com/t/activesupport-enumerable-sum-should-not-use-size/131299>\
**Category:** Ruby\
**Created:** [March 7, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/activesupport-enumerable-sum-should-not-use-size/131299 "2008-03-07T20:19:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 7, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/activesupport-enumerable-sum-should-not-use-size/131299/1 "2008-03-07T20:19:03Z")

</div>

ActiveSupport defines this for Enumerable#sum:

module Enumerable

```
def sum(identity = 0, &block)
  return identity unless size > 0

  if block_given?
    map(&block).sum
  else
    inject { |sum, element| sum + element }
  end
end

```

end

The use of #size shouldn’t be used in an Enumerable method --since it  
is not part of Enumerable’s defined interface… Ah, I was just  
about to ask what anyone thought the fix to this is, but it occurs to  
me that it might be:

```
def sum(identity = 0, &block)
  if block_given?
    map(&block).sum
  else
    inject { |sum, element| sum + element } || identity
  end
end

```

Look right?

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 7, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/activesupport-enumerable-sum-should-not-use-size/131299/2 "2008-03-07T21:35:09Z")

</div>

2008/3/7, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> inject { |sum, element| sum + element }
> def sum(identity = 0, &block)
> if block_given?
> map(&block).sum
> else
> inject { |sum, element| sum + element } || identity
> 
> ```

inject takes the identity as first parameter:

```
inject(identity) { |sum, element| sum + element }

```

and since they define Symbol#to\_proc, this can even be

```
inject(identity, &:+)

```

> ```
> end
> end
> 
> ```
> 
> Look right?
> 
> T.

Stefan

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 7, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/activesupport-enumerable-sum-should-not-use-size/131299/3 "2008-03-07T22:38:58Z")

</div>

Stefan L. wrote:

> inject takes the identity as first parameter:
> 
> ```
> inject(identity) { |sum, element| sum + element }
> 
> ```
> 
> and since they define Symbol#to\_proc, this can even be
> 
> ```
> inject(identity, &:+)
> 
> ```
> 
> > ```
> > end
> > end
> > 
> > ```
> > 
> > Look right?
> > 
> > T.
> 
> Stefan

This is very concise, but doesn’t have the same behavior as the original  
sum. This will start any sum with the identity and then build on it from  
there. Not that this is incorrect behavior, it’s just different from  
what was originally defined. For example:

a = [1,2,3]

original sum, with identity = 10, would return 6, whereas your version  
would return 16.

- Drew

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 8, 2008, 12:08am UTC](https://www.ruby-forum.com/t/activesupport-enumerable-sum-should-not-use-size/131299/4 "2008-03-08T00:08:45Z")

</div>

2008/3/7, Drew O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > ```
> > > end
> > > 
> > > ```
> 
> sum. This will start any sum with the identity and then build on it from  
> there. Not that this is incorrect behavior, it’s just different from  
> what was originally defined. For example:
> 
> a = [1,2,3]
> 
> original sum, with identity = 10, would return 6, whereas your version  
> would return 16.
> 
> - Drew

Hm, right. I find the name “identity” misleading in this case.

Stefan
