0

Here is my tree in rails application:

/page_downloader.rb

require "./lib/page_retriver.rb"

/lib/page_retriver

require 'open-uri'
require "page_validator"
require "content_cleaner"

class ResponseCanNotBeBlank < StandardError; end

class PageRetriver
    def self.retrive(url)
          ...
    end
end

/lib/page_validator.rb /lib/content_cleaner.rb

When I run the script *page_downloader.rb* I got an error:

/home/mariusz/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- page_validator (LoadError)
    from /home/mariusz/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/mariusz/Projects/webook/lib/page_retriver.rb:2:in `<top (required)>'
    from page_downloader.rb:1:in `load'
    from page_downloader.rb:1:in `<main>'

Is it possible to require relative libs in my ruby script without loading the whole rails environment?

2
  • do you use the bunlder? Jan 25, 2014 at 9:57
  • if yes, just add the Gemfile, yourproj.gemspec files to your project folder, and use bundle install/exec to run your executables Jan 25, 2014 at 10:17

1 Answer 1

2

There are a few ways to do this.

Load path

The way ruby looks for files is by searching it's load path. The load path is a collection of directories for which when you say 'require "my_file"' it will look through to find the matching file.

To see what is in your load path you can add:

puts $LOAD_PATH

or

puts $:

In your case you want to add the lib folder to your load path. So in your page_downloader.rb script is the one being executed you should add lib to your loadpath.

$:.unshift( File.expand_path('../lib', __FILE__) )

What this line does is prepend the lib directory to the load path. So this will be the first directory checked for files. From there you don't need to change your 'lib/page_retriever.rb' file.

Require absolute paths

A slightly ugly solution is to use absolute file paths for each file. Something like:

require file.expand_path('../lib/page_retriever', __FILE__)

Use require_relative

Require relative is a handy little tool to require files relative to the current file docs.

require_relative 'lib/page_retriever'

I hope that helps point in the right direction.

Edit:

If you are using this in a rails app the chances are that when you use it the rails environment will be loaded anyway. Rails by default adds 'lib' to the load path so if loaded from the rails environment it should work. If you are using this from a rake task or script that is something simply shipped with a rails app then you'll need to set up the load path yourself.

1
  • Thanks! Adding lib path to LOAD_PATH helped.
    – mario199
    Jan 25, 2014 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.