Search code examples
rubywindowspathrequire

Ruby: Require Fails To Import - Need To Set Root Directory


Forgive my inexperience with Ruby, but I am unable to run a script within a third-party project with the following structure:

˅ alpha
  ˅ lib
     ˅ bravo
        golf.rb
     ˅ charlie
        ˃ delta
           ˅ echo
              foxtrot.rb
              require "charlie/delta/echo/__init"
              __init.rb
              require "bravo/golf"

What should my command-line be to run the script, 'foxtrot.rb', as the following generates an error:

ruby "c:\arby\lib\bravo\charlie\delta\echo\foxtrot.rb"

"'require': cannot load such file -- charlie/delta/echo/__init (LoadError)"

Solution

  • If this is the code inside of __init.rb, it won't work.

    require "charlie/delta/echo/__init"
    __init.rb
    require "bravo/golf"
    

    require tells ruby to load the code inside a ruby file. In order for it to work, the files need to be organized correctly. You can also use require_relative but they still need a relative path from the file calling them. See What is the difference between require_relative and require in Ruby?