1

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)"
matekus
  • 778
  • 3
  • 14

1 Answers1

0

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?

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
  • Yes. Is it possible to set a root directory (here lib) from which the require imports are relatively positioned and, if so, how? – matekus Oct 23 '18 at 00:32
  • I can't imagine why anyone would create a script in such a chaotic file structure. Furthermore, I would surely not execute such a script without knowing what might be hidden in this crazy rats nest. – lacostenycoder Oct 23 '18 at 00:43
  • Forget the naming convention I used - original project is from an MIT research project - I am not worried about its provenance. Can I set a 'load path' root directory from which all requires are relatively located? Any experience with [Pathological](https://rubygems.org/gems/pathological)? – matekus Oct 23 '18 at 01:00
  • I haven't use that gem but give it a shot, though it has not been updated in a long time. If it doesn't work, try a fork which will support newer ruby versions. https://github.com/ooyala/pathological/pull/6 – lacostenycoder Oct 23 '18 at 01:05