需要相对路径

时间:2016-02-11 12:42:55

标签: ruby path require

编程语言:

红宝石

我有以下目录结构:

-program (dir)
--const.rb
--libs (dir)
---someClass.rb

我想include来自require / const.rb someClass.rb

我试过了:

(1)

$:.unshift File.expand_path(File.dirname.expand_path(__FILE__)))
require "../const"

(2)

$:.unshift File.expand_path(File.dirname.expand_path(__FILE__)))
require "./const"

(3)

require_relative "../const"

(4)

$:.unshift File.expand_path(File.dirname.expand_path(__FILE__)))
require_relative "../const"    (also with single quotes)

他们返回错误:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

require不进行相对路径扩展。你必须在传递之前扩展路径。

require File.expand_path("../const", __dir__)