# Inject error when no default starting value given?

**URL:** <https://www.ruby-forum.com/t/inject-error-when-no-default-starting-value-given/84270>\
**Category:** Ruby\
**Created:** [February 2, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/inject-error-when-no-default-starting-value-given/84270 "2007-02-02T20:41:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 2, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/inject-error-when-no-default-starting-value-given/84270/1 "2007-02-02T20:41:39Z")

</div>

I have an Event model which has\_many :attendees. My Attendee model has a  
column called participants, which is an integer representing the number  
of participants in the attendees party. I want to sum the total number  
of participants for a given event. This works fine if I just the default  
option of 0 for the method inject, but inject fails if you don’t give it  
a default. In ruby, injects normal behavior will default the sum to 0 if  
you don’t provide an argument. See my console output below:

> > myevent = Event.find(1)  
> > =\> #\<Event:0x34d95ec @attributes={“title”=\>“Canned Food Drive”,  
> > “date”=\>“2007-02-01 12:00:00”, “id”=\>“1”, “description”=\>“We need a  
> > least 20 people to sign up for the canned food drive. This will could  
> > toward service hours and will be a great event to help younger scouts  
> > get involved with the community. We will be collecting, sorting and  
> > delivering donated cans.”, “rsvp\_date”=\>“2007-01-15”,  
> > “permission\_url”=\>"/docs/permission.jpg", “location”=\>“Saint David’s”}\>
> 
> > myevent.attendees.inject(0){|sum,att| sum = att.participants}  
> > =\> 1
> 
> > myevent.attendees.inject{|sum,att| sum = att.participants}  
> > =\> #\<Attendee:0x34cc7ac @attributes={“name”=\>“Drew O.”,  
> > “event\_id”=\>“1”, “participants”=\>“1”, “contact”=\>“847.942.2606”,  
> > “id”=\>“1”}\>

As you can see, if a default of 0 is not provided, inject returns an  
instance of the Attendee class rather than the value in sum. Very  
strange…

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 2, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/inject-error-when-no-default-starting-value-given/84270/2 "2007-02-02T20:42:07Z")

</div>

Misposted, moving to the rails forum.

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [February 2, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/inject-error-when-no-default-starting-value-given/84270/3 "2007-02-02T23:07:31Z")

</div>

On Feb 2, 2007, at 2:41 PM, Drew O. wrote:

> As you can see, if a default of 0 is not provided, inject returns an  
> instance of the Attendee class rather than the value in sum. Very  
> strange…

You gave a Rails example, but it is really a generic Ruby question.

If you don’t give an argument to inject, it will use the first item  
in the collection as the starting accumulator:

irb\> [1,2,4].inject { |sum,i| p [sum,i]; sum + i }  
[1, 2]  
[3, 4]  
=\> 7
