marcamillion marcamillion - 2 months ago 15
Ruby Question

Ruby `require': cannot load such file (LoadError)

I have a directory structure that looks like this:

- lib
- yp-crawler (directory)
- file-a.rb
- file-b.rb
- file-c.rb
- yp-crawler.rb


My
lib/yp-crawler.rb
file looks like this:

require "yp-crawler/file-c"
require "yp-crawler/file-b"
require "yp-crawler/file-a"

module YPCrawler
end


When I try to run my file at the command line by doing this:

ruby lib/yp-crawler.rb


I get this error:

`require': cannot load such file -- yp-crawler/file-c (LoadError)
from .rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from lib/yp-crawler.rb:1:in `<main>'


What could be causing this?

Answer

According to API Dock, require_relative is what you need.

Ruby tries to load the library named string relative to the requiring file’s path. If the file’s path cannot be determined a LoadError is raised. If a file is loaded true is returned and false otherwise.

So all you have to do is

require_relative "file-a"
require_relative "file-b"
require_relative "file-c"