# Where to put algorithm class

**URL:** <https://www.ruby-forum.com/t/where-to-put-algorithm-class/82428>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/where-to-put-algorithm-class/82428 "2007-01-19T16:30:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/where-to-put-algorithm-class/82428/1 "2007-01-19T16:30:21Z")

</div>

I have a large class of algorithms (50-100 methods) used in calculating  
some results for a model class. I have created a separate algorithm.rb  
file which contains the class. Where should this file go ? Should it  
even be a module instead of a class (though I do need it to hold class  
variables during the calculations)? Finally, how do I call it from the  
model active record class ?

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/where-to-put-algorithm-class/82428/2 "2007-01-19T16:31:26Z")

</div>

john wrote:

> I have a large class of algorithms (50-100 methods) used in calculating  
> some results for a model class. I have created a separate algorithm.rb  
> file which contains the class. Where should this file go ?

If it doesn’t use ActiveView or ActiveRecord, it goes in the lib/  
folder.

> Should it  
> even be a module instead of a class (though I do need it to hold class  
> variables during the calculations)?

We use Modules when we want to isolate the name of a class, and/or when  
we  
want the Module to mix-in with other classes. If you don’t need those,  
then  
do what’s simplest. Plop a Class into the lib/ folder, and worry about  
name  
collisions when they happen.

(If you publish this class, however, that formula no longer applies, and  
you  
should put the class in a module with a name that others are not likely  
to  
use.😉

> Finally, how do I call it from the  
> model active record class ?

Given lib/algorithm.rb, write:

```
require 'algorithm'

```

up at the top of the Model that uses it.

And toss your test cases into test/unit . (You do _have_ test cases,  
don’t  
you?😉

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/where-to-put-algorithm-class/82428/3 "2007-01-19T16:31:57Z")

</div>

If you are using your algorithms in places besides your webapp, why  
not make yourself an algorithms gem and install it on your system?

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [September 25, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/where-to-put-algorithm-class/82428/4 "2007-09-25T22:41:40Z")

</div>

Phlip wrote:

> If it doesn’t use ActiveView or ActiveRecord, it goes in the lib/ folder.

> Given lib/algorithm.rb, write:
> 
> ```
> require 'algorithm'
> 
> ```

okay

> And toss your test cases into test/unit . (You do _have_ test cases, don’t  
> you?😉

How do I set up a test stub for the separate class file. To get code  
converted to ruby, I had made it a separate controller and yes, have  
been testing every method.
