# Best way to extract common methods

**URL:** <https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266>\
**Category:** Rails\
**Created:** [February 12, 2007, 4:21am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266 "2007-02-12T04:21:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 4:21am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/1 "2007-02-12T04:21:10Z")

</div>

Hello,

I have a group of methods that I use across numerous controllers. I  
want to put the methods in their own file and call them in from the  
controllers. My (limited) understanding is that modules are the  
solution to this problem. The methods could be considered class  
methods. For example, there is a method that accepts a string and  
returns the count of each character (a, b, c, etc).

What is the best way to make these methods available to all the  
controllers that might want to use them? In Objective-C (my other  
exposure to MVC) I would create a class of the methods, include it and  
call the class methods. What is the corresponding approach in RoR?

Many thanks,  
Ryan G.

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 5:17am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/2 "2007-02-12T05:17:16Z")

</div>

Hi Ryan,

Ryan G. wrote:

> I have a group of methods that I use across  
> numerous controllers. I want to put the methods  
> in their own file and call them in from the  
> controllers.

Just put your methods in application.rb. Any method there is available  
to  
all controllers in the app.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 5:47am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/3 "2007-02-12T05:47:17Z")

</div>

On 2/11/07, Ryan G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I have a group of methods that I use across numerous controllers. I  
> want to put the methods in their own file and call them in from the  
> controllers. My (limited) understanding is that modules are the  
> solution to this problem. The methods could be considered class  
> methods. For example, there is a method that accepts a string and  
> returns the count of each character (a, b, c, etc).

Modules are indeed a good way to do this. You could, as one poster  
suggested, throw them all into application.rb. But you might find  
that file getting big and unwieldy. An alternative is to group them  
into a well-named module or set of modules and then to include those  
modules in any controllers (or any other context) in which they’re  
needed. You could put the module in a file in, say, your lib/  
directory, require that file in environment.rb and then where ever it  
makes sense, do:

class MyController \< ApplicationController  
include MyWellNamedModule  
def some\_action  
some\_method\_which\_counts\_characters\_in\_a\_string(“a string”)  
end  
end

Chad

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 5:58am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/4 "2007-02-12T05:58:15Z")

</div>

Thank you for the suggestions. I have implemented the following module  
approach but it keeps raising an error. I followed the example in the  
pick axe book.

I have a module named reading.rb in my /lib directory. It contains the  
following (modified example to keep things simple, it still doesnt  
work).

module Reading  
def Reading.text\_length(text)  
length = text.length  
return length  
end  
end

Then, I have a controller class with the following code.

require ‘reading’  
class ProcessController \< ApplicationController  
def analyze  
submission = params[:submission]  
@text = submission[:text]  
@text\_length = Reading.reading(@text)  
end  
end

It doesn’t use the include statement as I am following the first example  
in the module chapter of the pick axe book.

However, when I run this, I get the following error.

undefined method `reading’ for Reading:Module

If I take the method from the module and put it right in the controller  
(and change the call from Reading.reading to just reading), it works  
great.

Any thoughts?

Many thanks,  
Ryan G.

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 6:04am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/5 "2007-02-12T06:04:54Z")

</div>

Whoops, you are correct. Unfortunately, that is a typo from me trying  
to simplify the code for the forum post and that error is not in my  
actual code.

Thanks anyway,  
Rg

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 6:17am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/6 "2007-02-12T06:17:08Z")

</div>

I solved the issue. As you can see in my above example, I was using  
length as a variable name in the module. As soon as I changed that to  
something else it worked great. Sorry for wasting everyones time.

Rg

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [February 12, 2007, 6:01am UTC](https://www.ruby-forum.com/t/best-way-to-extract-common-methods/85266/7 "2007-02-12T06:01:19Z")

</div>

On 2/11/07, Ryan G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def Reading.text_length(text)
> submission = params[:submission]
> 
> ```
> 
> undefined method `reading’ for Reading:Module
> 
> If I take the method from the module and put it right in the controller  
> (and change the call from Reading.reading to just reading), it works  
> great.
> 
> Any thoughts?

You called the method something different from the name you’re using  
to call it. I think you just made a simple mistake. Look closely at  
the definition vs. what you’re calling in the controller 🙂

Chad
