# Best way to add a method to the Array class

**URL:** <https://www.ruby-forum.com/t/best-way-to-add-a-method-to-the-array-class/72831>\
**Category:** Rails\
**Created:** [October 19, 2006, 8:25am UTC](https://www.ruby-forum.com/t/best-way-to-add-a-method-to-the-array-class/72831 "2006-10-19T08:25:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 19, 2006, 8:25am UTC](https://www.ruby-forum.com/t/best-way-to-add-a-method-to-the-array-class/72831/1 "2006-10-19T08:25:55Z")

</div>

Hey guys, whats the best way to add a method to the array class so that  
it will act on an array? Can you give an example?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 19, 2006, 8:50am UTC](https://www.ruby-forum.com/t/best-way-to-add-a-method-to-the-array-class/72831/2 "2006-10-19T08:50:09Z")

</div>

On 10/19/06, Eric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey guys, whats the best way to add a method to the array class so that  
> it will act on an array? Can you give an example?
> 
> –

Ruby classes are open, so you can simply do:

class Array  
def sum  
inject(0) { |sum,v| sum+=v }  
end  
end

You can also redefine existing methods, but you’ll have to be very  
careful with that. alias is your friend.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 19, 2006, 9:12am UTC](https://www.ruby-forum.com/t/best-way-to-add-a-method-to-the-array-class/72831/3 "2006-10-19T09:12:30Z")

</div>

where would that code snippet go?

In the application controller?

Max M. wrote:

> On 10/19/06, Eric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hey guys, whats the best way to add a method to the array class so that  
> > it will act on an array? Can you give an example?
> > 
> > –
> 
> Ruby classes are open, so you can simply do:
> 
> class Array  
> def sum  
> inject(0) { |sum,v| sum+=v }  
> end  
> end
> 
> You can also redefine existing methods, but you’ll have to be very  
> careful with that. alias is your friend.
> 
> Cheers,  
> Max

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 20, 2006, 8:07am UTC](https://www.ruby-forum.com/t/best-way-to-add-a-method-to-the-array-class/72831/4 "2006-10-20T08:07:07Z")

</div>

On 10/19/06, Eric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> where would that code snippet go?
> 
> In the application controller?

No. Create a new file under lib, for example array\_extensions.rb and  
you should be fine.

Max
