Forum: Ruby on Rails Where to put algorithm class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
john (Guest)
on 2007-01-19 17:30
(Received via mailing list)
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 ?
Phlip (Guest)
on 2007-01-19 17:31
(Received via mailing list)
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
  http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Brian B. (Guest)
on 2007-01-19 17:31
(Received via mailing list)
If you are using your algorithms in places besides your webapp, why
not make yourself an algorithms gem and install it on your system?
john (Guest)
on 2007-09-26 00:41
(Received via mailing list)
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.
This topic is locked and can not be replied to.