# Dummy question about method in class

**URL:** <https://www.ruby-forum.com/t/dummy-question-about-method-in-class/160664>\
**Category:** Ruby\
**Created:** [February 18, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/dummy-question-about-method-in-class/160664 "2009-02-18T16:36:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 18, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/dummy-question-about-method-in-class/160664/1 "2009-02-18T16:36:11Z")

</div>

Dear all

I have a dummy question.

[1,2,3,4,5].inject { |sum,n| sum+n }  
The inject method in this case is to calculate the sum of array.

I searched the RDoc. Why the inject method is belongs to Enumerable  
Class, but not Array Class?

Many thanks  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 18, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/dummy-question-about-method-in-class/160664/2 "2009-02-18T16:43:27Z")

</div>

On Feb 18, 2009, at 4:36 PM, Valentino L. wrote:

> [1,2,3,4,5].inject { |sum,n| sum+n }  
> The inject method in this case is to calculate the sum of array.
> 
> I searched the RDoc. Why the inject method is belongs to Enumerable  
> Class, but not Array Class?

Enumerable is a Module, not a Class. It can be mixed into other  
classes, so the Class gains the Methods of the module. Arrays uses  
Enumerable instead of implementing #inject on its own, so they respond  
to Enumerables #inject.

As a short presentation, i redefine Enumerable#inspect to do something  
different (never try this at home!):

===  
module Enumerable  
def inject  
puts “hey”  
end  
end

# [].inject #=\> puts “hey” now

Regards,  
Florian G.

–  
Florian G.

smtp: removed\_email\_address@domain.invalid  
jabber: removed\_email\_address@domain.invalid  
gpg: 533148E2

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 18, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/dummy-question-about-method-in-class/160664/3 "2009-02-18T22:52:20Z")

</div>

On 18.02.2009 16:36, Valentino L. wrote:

> [1,2,3,4,5].inject { |sum,n| sum+n }  
> The inject method in this case is to calculate the sum of array.

Additional to Florian’s good explanation: be aware that the version you  
posted has a special property: it will return nil for empty Enumerables.

Consider this version which in most cases is the one you want (an empty  
list summed up results in 0):

[1,2,3,4,5].inject(0) { |sum,n| sum+n }

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [February 19, 2009, 10:13am UTC](https://www.ruby-forum.com/t/dummy-question-about-method-in-class/160664/4 "2009-02-19T10:13:42Z")

</div>

Valentino L. wrote:

> I have a dummy question.
> 
> [1,2,3,4,5].inject { |sum,n| sum+n }  
> The inject method in this case is to calculate the sum of array.
> 
> I searched the RDoc. Why the inject method is belongs to Enumerable  
> Class, but not Array Class?

The Enumerable module can be shared by many classes. Any class which  
implements its own ‘each’ method can mixin Enumerable and gain a whole  
load of functionality.

For example:

class Fib  
include Enumerable  
def initialize(a,b,count)  
@a, @b, @count = a, b, count  
end  
def each  
@count.times do  
yield @a  
@a, @b = @b, @a+@b  
end  
end  
end

p Fib.new(1,1,10).inspect  
p Fib.new(1,1,10).max  
p Fib.new(1,1,10).inject(0) { |sum,n| sum+n }
