Forum: Ruby on Rails what's the right way to require a file?

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.
67800acb8e18674976b4a90b7463660f?d=identicon&s=25 Luca Pireddu (Guest)
on 2006-01-20 01:42
(Received via mailing list)
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 <controller not set>#<action not set>, 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 <controller not set>#<action not set>

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
00e3a96684ab390a350b0271e98741d3?d=identicon&s=25 Nshbrown Nshbrown (nshb)
on 2006-01-20 01:48
(Received via mailing list)
>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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Nathaniel S. H. Brown                           http://nshb.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67800acb8e18674976b4a90b7463660f?d=identicon&s=25 Luca Pireddu (Guest)
on 2006-01-20 02:09
(Received via mailing list)
On Thursday 19 January 2006 17:08, Nathaniel S. H. Brown wrote:
>  Nathaniel S. H. Brown                           http://nshb.net
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

Luca
This topic is locked and can not be replied to.