# How to include classes in extensions?

**URL:** <https://www.ruby-forum.com/t/how-to-include-classes-in-extensions/122418>\
**Category:** Radiant CMS\
**Created:** [December 7, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/how-to-include-classes-in-extensions/122418 "2007-12-07T13:33:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/how-to-include-classes-in-extensions/122418/1 "2007-12-07T13:33:37Z")

</div>

Hi,

I would like to access this snippet of code from within my extension  
tags:

class Numeric  
def ordinal  
cardinal = self.to\_i.abs  
if (10…20).include?(cardinal) then  
cardinal.to\_s \<\< ‘th’  
else  
cardinal.to\_s \<\< %w{th st nd rd th th th th th th}[cardinal % 10]  
end  
end  
end

As the tags get called on the page I have tried adding the above to:

module StartDate::PageExtensions  
class Numeric \< Page

```
 def ordinal
   cardinal = self.to_i.abs
   if (10...20).include?(cardinal) then
     cardinal.to_s << 'th'
   else
     cardinal.to_s << %w{th st nd rd th th th th th th}[cardinal %

```

10]  
end  
end

end  
end

which is called from:

Page.send :include, StartDate::PageExtensions

The tag works fine when I don’t call the new method, but when I do I’m  
getting an “undefined method ‘ordinal’ error”, so I must be defining  
it wrong, but how do I do it right?

Any advice?

Thanks,

Dominic

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/how-to-include-classes-in-extensions/122418/2 "2007-12-07T15:10:29Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> else
> cardinal.to_s << %w{th st nd rd th th th th th th}[cardinal % 10]
> end
> 
> ```
> 
> end  
> end

This by itself in a file should be fine. However, if you want to make  
sure you’re using the existing Numeric class and not creating a new one,  
change class Numeric … end to Numeric.class\_eval do … end.

> ```
> cardinal.to_s << %w{th st nd rd th th th th th th}[cardinal %  
> 
> ```
> 
> 10]  
> end  
> end
> 
> end  
> end

Here you’ve made a class StartDate::PageExtensions::Numeric which  
inherits from Page. I don’t think this is what you want to do. You  
want to override the existing Ruby Numeric class.

I would take your first snippet, put it in a file  
lib/numeric\_extensions.rb, then explicitly require it from your  
extension’s activate method.

Hope this helps!

Sean

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/how-to-include-classes-in-extensions/122418/3 "2007-12-07T15:19:15Z")

</div>

Hi Dominic,

First off, I think what you’re trying to do is already done by a Rails  
core extension to Integer. See:

[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Integer/Inflections.html](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Integer/Inflections.html)

If you still want to do something like this, it needs to be more like  
(and I haven’t tested this code):

module Ordinalizable  
def ordinalize  
cardinal = self.to\_i.abs  
if (10…20).include?(cardinal) then  
cardinal.to\_s \<\< ‘th’  
else  
cardinal.to\_s \<\< %w{th st nd rd th th th th th th}[cardinal % 10]  
end  
end  
end

# Only Integers should be ordinalized

Integer.send :include, Ordinalizable

Stick this in your extension’s lib directory and explicitly require it  
from the extension file.

Your first example where you monkey-patch Numeric should have worked  
(depending of course on if the file was loaded), however, like I said  
above, Floats and most of the other Numerics probably shouldn’t be  
ordinalized. I think module mixin is generally cleaner from a design  
standpoint (opinions may vary), though.

Your second one has a couple of problems, which I point out not out of  
toolishness, but in the hope that it’s helpful: 😉

1. Numeric doesn’t need to inherit from Page. This is only necessary  
for new Page types, which will appear in the Page Type select box.

2. Putting a class inside an extension is often a good way to keep  
your namespace clean, but you probably shouldn’t do it for anything  
that inherits from ActiveRecord::Base.

3. It looks like what you’ve got is a case of mixed metaphors here:  
you’re trying to mixin a module to put an instance method into a  
class, and you’re trying to monkey-patch the class directly. Both are  
fine on their own (with varying opinions on cleanliness), but neither  
will work this way.

4. You don’t want to include the module into Page. Since ordinalize  
assumes that self is an Integer, it doesn’t make any sense for it to  
operate on a Page. If, however, you defined a module with a number of  
tags (which it sounds like you’re also trying to do elsewhere), that  
is what you want to do to get the tags into Page.

Hope this helps,  
Andrew

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/how-to-include-classes-in-extensions/122418/4 "2007-12-07T20:23:21Z")

</div>

Many thanks for both your detailed answers, they were indeed very  
helpful. I went ahead with the mixin module as I also wanted the  
ordinals styled with 's. After some thought I should imagine a  
much cleaner way would be by defining a regexp for the date string and  
running that from the tags, but as you can probably tell I’m learning  
as I go. I’m not sure well formed the tags are on the other side but  
it’s seems to be working as I wanted.

For your pleasure:

```
   start_date = page.start_date
   sdate = start_date.strftime("%A") + " " +

```

start\_date.strftime("%d").to\_i.ordinalize + " " +  
start\_date.strftime("%B")  
sdate

Its probably not the place to ask, but if anyone knows a simple way to  
replace those ordinals in the string using regexp I would be very  
gratefiul!

Thanks once again for your all help,

Dominic
