# What's the right way to require a file?

**URL:** <https://www.ruby-forum.com/t/whats-the-right-way-to-require-a-file/52899>\
**Category:** Rails\
**Created:** [January 20, 2006, 1:42am UTC](https://www.ruby-forum.com/t/whats-the-right-way-to-require-a-file/52899 "2006-01-20T01:42:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Pireddu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8491ac/32.png) [@Luca\_Pireddu](https://www.ruby-forum.com/u/Luca_Pireddu)\
**Post date:** [January 20, 2006, 1:42am UTC](https://www.ruby-forum.com/t/whats-the-right-way-to-require-a-file/52899/1 "2006-01-20T01:42:43Z")

</div>

I’m having a problem importing a class from another project (not a RoR  
application). What’s the right way to require such a file?

I have a class ModelPathwayObject. It’s in  
#{RAILS\_ROOT}/…/lib/model\_pathway\_object.rb , so I add that path to  
config.load\_paths. In config/environment.rb:

Rails::Initializer.run do |config|  
config.load\_paths += %W( #{RAILS\_ROOT}/…/lib )  
end

I then tried to require the file from config/environment.rb by appending  
require ‘model\_pathway\_object’

With these changes WEBrick wouldn’t start:  
[luca@zirellu pa\_web] ./script/server  
=\> Booting WEBrick…  
[luca@zirellu pa\_web]

So I moved the require to the top of my controller’s file. Now WEBrick  
starts, but the first time I place a request to the controller I get an  
error  
(Gem::LoadError in #, trace at the  
bottom  
of this message), but afterwards it works fine.

I figure I’m doing something wrong, otherwise the thing would work from  
the  
first request. Can someone tell me what the right way to do this is?  
Thanks  
in advance.

Luca

=============================  
Trace on first request to controller:

Gem::LoadError in #

Could not find RubyGem gnuplot (\> 0.0.0)

RAILS\_ROOT: script/…/config/…

/usr/local/lib/site\_ruby/1.8/rubygems.rb:194:in `report_activate_error' /usr/local/lib/site_ruby/1.8/rubygems.rb:136:in`activate’  
/usr/local/lib/site\_ruby/1.8/rubygems.rb:37:in  
`require_gem_with_options' /usr/local/lib/site_ruby/1.8/rubygems.rb:31:in`require\_gem’  
./script/…/config/…/…/lib/plot\_utils.rb:2  
./script/…/config/…/…/lib/predictor\_classes.rb:9  
./script/…/config/…/lib/model\_pathway\_object.rb:6  
./script/…/config/…/app/controllers/model\_pathway\_controller.rb:1  
routing.rb:234:in `traverse_to_controller' generated/routing/recognition.rb:7:in`eval’  
generated/routing/recognition.rb:7:in `recognize\_path’

This error occured while loading the following files:  
script/…/config/…/app/controllers/model\_pathway\_controller.rb  
model\_pathway\_object  
predictor\_classes  
plot\_utils

---

<div class="post-metadata">

**Author:** ![Luca\_Pireddu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8491ac/32.png) [@Luca\_Pireddu](https://www.ruby-forum.com/u/Luca_Pireddu)\
**Post date:** [January 20, 2006, 1:48am UTC](https://www.ruby-forum.com/t/whats-the-right-way-to-require-a-file/52899/2 "2006-01-20T01:48:45Z")

</div>

> From what I understand, all the files in lib are automatically loaded, you  
> don’t need to require them specifically. The load path that you see,  
> scans  
> the entire directory for rb files and includes them.

Try removing the require, and simply just use the model\_pathway\_object  
within your app.

-Nb

```auto
 Nathaniel S. H. Brown http://nshb.net

```

---

<div class="post-metadata">

**Author:** ![Luca\_Pireddu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8491ac/32.png) [@Luca\_Pireddu](https://www.ruby-forum.com/u/Luca_Pireddu)\
**Post date:** [January 20, 2006, 2:09am UTC](https://www.ruby-forum.com/t/whats-the-right-way-to-require-a-file/52899/3 "2006-01-20T02:09:54Z")

</div>

On Thursday 19 January 2006 17:08, Nathaniel S. H. Brown wrote:

> Nathaniel S. H. Brown [http://nshb.net](http://nshb.net)
> 
> ```auto
> 
> ```

You’re right. Thanks for the tip. The file is required automatically.

Luca
