I'm working with Ruby 1.8 and I have script that I want to call but it's in a parent folder. Below is the structure:
maindir/
neededscript.rb
subdir/
subdir2/
myscript.rb
How can I require neededscript.rb
from inside myscript.rb
?
I'm working with Ruby 1.8 and I have script that I want to call but it's in a parent folder. Below is the structure:
maindir/
neededscript.rb
subdir/
subdir2/
myscript.rb
How can I require neededscript.rb
from inside myscript.rb
?
In Ruby >=1.9 you can use the require_relative
method
require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.
require_relative '../../neededscript.rb'
You can also add the path to the ruby libs path by adding these lines to myscript.rb before doing the require
binpath = File.dirname( __FILE__ )
$:.unshift File.expand_path( File.join( binpath, ".." ) )
This is what I've done instead:
File.expand_path("../../neededsript.rb",File.dirname(__FILE__))