# Extending Array class with a method

**URL:** <https://www.ruby-forum.com/t/extending-array-class-with-a-method/107175>\
**Category:** Rails\
**Created:** [June 18, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/extending-array-class-with-a-method/107175 "2007-06-18T20:15:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Nevile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/48db29/32.png) [@Ben\_Nevile](https://www.ruby-forum.com/u/Ben_Nevile)\
**Post date:** [June 18, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/extending-array-class-with-a-method/107175/1 "2007-06-18T20:15:55Z")

</div>

Hi,

Let’s say I have an ActiveRecord::Base class and I want to be able to  
call a method on an Array of these items. For instance, perhaps  
there’s a class called Country and another called business, and I’d  
like to be able to do something like

great\_white\_north = Country.find\_by\_name(‘Canada’)  
canuck\_enterprises = great\_white\_north.businesses

countries = Country.find\_by\_name([‘Canada’,‘UK’,‘Australia’)  
we\_love\_the\_queen\_enterprises = countries.businesses

So I need to somehow extend the Array class I think. Tips?  
Ben

---

<div class="post-metadata">

**Author:** ![Ben\_Nevile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/48db29/32.png) [@Ben\_Nevile](https://www.ruby-forum.com/u/Ben_Nevile)\
**Post date:** [June 18, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/extending-array-class-with-a-method/107175/2 "2007-06-18T20:23:56Z")

</div>

Hi Ben,

> great\_white\_north = Country.find\_by\_name(‘Canada’)  
> canuck\_enterprises = great\_white\_north.businesses
> 
> countries = Country.find\_by\_name([‘Canada’,‘UK’,‘Australia’)  
> we\_love\_the\_queen\_enterprises = countries.businesses  
> we\_love\_the\_queen\_enterprises = countries.map { |c| c.businesses }

> So I need to somehow extend the Array class I think. Tips?  
> No you don’t … 🙂

Regards Florian

---

<div class="post-metadata">

**Author:** ![Ben\_Nevile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/48db29/32.png) [@Ben\_Nevile](https://www.ruby-forum.com/u/Ben_Nevile)\
**Post date:** [June 18, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/extending-array-class-with-a-method/107175/3 "2007-06-18T20:35:59Z")

</div>

On Jun 18, 2007, at 2:03 PM, Ben Nevile wrote:

> Hi,
> 
> Let’s say I have an ActiveRecord::Base class and I want to be able to  
> call a method on an Array of these items. For instance, perhaps  
> there’s a class called Country and another called business, and I’d  
> like to be able to do something like
> 
> great\_white\_north = Country.find\_by\_name(‘Canada’)  
> canuck\_enterprises = great\_white\_north.businesses

Assuming you have:

class Country \< ActiveRecord::Base  
has\_many :businesses  
end

Then you’re practically already there!

> countries = Country.find\_by\_name([‘Canada’,‘UK’,‘Australia’)  
> we\_love\_the\_queen\_enterprises = countries.businesses

countries = Country.find(:all, :conditions =\> [ ‘name IN (?)’,  
[‘Canada’, ‘UK’, ‘Australia’] ])  
we\_love\_the\_queen\_enterprises = countries.map(&:businesses).flatten

> So I need to somehow extend the Array class I think. Tips?  
> Ben

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
