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?

有帮助吗?

解决方案 3

This is what I've done instead:

File.expand_path("../../neededsript.rb",File.dirname(__FILE__))

其他提示

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, ".." ) )
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top