# How to add a method to an existing class and use it in rails environment

**URL:** <https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121>\
**Category:** Rails\
**Created:** [July 3, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121 "2008-07-03T17:30:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pbn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pbn](https://www.ruby-forum.com/u/pbn)\
**Post date:** [July 3, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121/1 "2008-07-03T17:30:37Z")

</div>

Hi,

I am using rails 2.0.2 version and I have a need to add a method to an  
existing class and use it in the rails environment. For example, I  
would like to add a method to Numeric class, so I coded it as follows

class Numeric  
def radians  
self \* Math::PI/180  
end  
end

and then placed it under /lib directory. However I am  
not able use this new method in my apps controller, ruby throws no  
method found error.

Did I do something wrong?

Thanks  
Babu

---

<div class="post-metadata">

**Author:** ![pbn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pbn](https://www.ruby-forum.com/u/pbn)\
**Post date:** [July 3, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121/2 "2008-07-03T17:47:01Z")

</div>

the code is ok I think.  
but if it’s in lib, you would have to require it before you can use it  
so if the file is named numeric.rb  
then  
require “numeric”  
either in environment.rb or in the controller or mode where it is used

another option is to put the file in config/initializers  
this should be loaded by default (though I haven’t used this yet)

---

<div class="post-metadata">

**Author:** ![pbn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pbn](https://www.ruby-forum.com/u/pbn)\
**Post date:** [July 3, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121/3 "2008-07-03T18:15:49Z")

</div>

If it goes in environment.rb (which is where I put anything that will be  
needed anywhere in the application), you’ll need to restart your server.  
Can’t speak about the initializers. Haven’t used them yet.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pbn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pbn](https://www.ruby-forum.com/u/pbn)\
**Post date:** [July 3, 2008, 7:01pm UTC](https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121/4 "2008-07-03T19:01:32Z")

</div>

Thanks for the replies. I tried placing the class definition directly  
in environment.rb, it works but any updates to  
the definition requires server restart.

Probably I’ll try placing the .rb file under libs directory and  
require statement in controller or directly in the model.

Thanks again  
-Babu

On Jul 3, 10:15 am, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pbn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pbn](https://www.ruby-forum.com/u/pbn)\
**Post date:** [July 3, 2008, 9:57pm UTC](https://www.ruby-forum.com/t/how-to-add-a-method-to-an-existing-class-and-use-it-in-rails-environment/142121/5 "2008-07-03T21:57:33Z")

</div>

Phillip K. wrote:

> If it goes in environment.rb (which is where I put anything that will be  
> needed anywhere in the application), you’ll need to restart your server.  
> Can’t speak about the initializers. Haven’t used them yet.

Oh, I should clarify that I don’t mean to put the actual method  
definitions in environment.rb. I always put those in a file in lib/.  
What I meant was if the require were to go in environment.rb,  
then a server restart is required when you change lib/file.

Sorry for the confusion.

Phillip
