# Implanting a new method for a class

**URL:** <https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232>\
**Category:** Ruby\
**Created:** [February 12, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232 "2006-02-12T21:21:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [February 12, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232/1 "2006-02-12T21:21:41Z")

</div>

Hi there, I have a question regarding how to implant a new method into  
the  
class Array. I would like to write a ruby file that sums all of the  
digits  
in an array. I have tried this:

class Array  
def Array.sum  
until (Array.empty?)  
arraySum += Array.pop  
end  
puts arraySum  
end  
end

Gave no error when I ran the code. My problem occur when i try to use  
this  
method:

require “Library/Array\_sum.rb”  
a = [1…100]  
a.sum

I get a NoMethodError on sum. Can anybody tell me what I did wrong here?  
Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [February 12, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232/2 "2006-02-12T21:49:28Z")

</div>

On 2/12/06, Jeppe J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> Thanks in advance 🙂  
> You are confusing classes and instances of classes. You are defining  
> methods for the Array class, not for actual array instances.

class A  
def A.foo  
puts “Class method of #{self.inspect}, instance of class  
#{self.class}”  
end  
def foo  
puts “Instance method for #{self.inspect}, of class #{self.class}”  
end  
end

A.foo # prints “Class method of A, instance of class Class”  
a = A.new  
a.foo # prints “Instance method of \<#A:0xb7ce9000\> of class A”

You can implement sum like so:

class Array  
def sum  
total = 0  
# self refers to the current array instance  
self.each { |elem| total += elem }  
total  
end

```
# but you can also implement sum on the class Array itself. But 

```

then  
# you need to provide the array, that you want to know the sum of  
#  
def Array.sum arr  
arr.inject(0) { |total, elem| total += elem }  
end  
end

puts [1,2,3,4].sum  
puts Array.sum([1,2,3,4,5])

be careful with “pop”, it changes the Array!

def sum!(arr)  
total = 0  
total += arr.pop until a.empty?  
total  
end

arr = [5,4,3]  
p arr.sum! #=\> 12  
p arr #=\> [] arr is now empty!

-Levin

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [February 12, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232/3 "2006-02-12T21:55:54Z")

</div>

Jeppe J. wrote:

> Hi there, I have a question regarding how to implant a new method into  
> the  
> class Array. I would like to write a ruby file that sums all of the  
> digits  
> in an array. I have tried this:
> 
> class Array  
> def Array.sum  
> until (Array.empty?)  
> arraySum += Array.pop  
> end  
> puts arraySum  
> end  
> end
> 
> Gave no error when I ran the code. My problem occur when i try to use  
> this  
> method:
> 
> require “Library/Array\_sum.rb”  
> a = [1…100]  
> a.sum
> 
> I get a NoMethodError on sum. Can anybody tell me what I did wrong here?  
> Thanks in advance 🙂

Jeppe,  
You have a number of problems here. First, if what you are really trying  
to do is to sum the array, rather than just learn how to add methods to  
a class, then all you have to do is

a = [1,2,3]  
a.inject { |v,n| v+n }  
=\> 6

Second, you created your function as a class method rather than an  
instance method by calling it Array.sum. All you need is  
def sum  
…  
end

This may be why you are getting the MethodNotFound, since the instance  
of array “a” does not have this method - the class Array does.

Finally, a = [1…100] will give you an array with the first value being  
the range [1…100], not an array with all the values from 1 to 100,  
[1,2,3…].

Two suggestions: learn to use irb to see what happens when you call  
methods, and (2) pick up Programming Ruby by Dave T. if you have not  
already done so. It explains all of this in detail.

Hope this helps,  
Keith

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [February 12, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232/4 "2006-02-12T22:05:37Z")

</div>

Thank you, for sorting things out for me 🙂

2006/2/12, Levin A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![braas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@braas](https://www.ruby-forum.com/u/braas)\
**Post date:** [February 12, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/implanting-a-new-method-for-a-class/66232/5 "2006-02-12T21:57:03Z")

</div>

Jeppe J. wrote:

> …  
> I get a NoMethodError on sum. Can anybody tell me what I did wrong here?  
> Thanks in advance 🙂

You’ve defined a class method (e.g. Array.sum)

You need to do this:

class Array  
def sum  
until (Array.empty?)  
…

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools
