【问题标题】:Requiring with relative path要求相对路径
【发布时间】:2016-05-22 05:59:31
【问题描述】:

编程语言:

红宝石

我的目录结构如下:

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

我想从someClass.rbinclude/require const.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)

有什么想法吗?

【问题讨论】:

  • 顺便说一句,require_relative '../const' 为我工作。
  • 嗯,奇怪,我会仔细检查一下,我会回复你的

标签: ruby path require


【解决方案1】:

require 不进行相对路径扩展。您必须先扩展路径,然后再传递给它。

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

【讨论】:

  • 试过了,但还没用,还在看,但开始的规则不是已经解决了吗? => $:.unshift File.extend_path(File.dirname.expand_path(FILE))))
  • 文件的未定义“extend_path”方法:Class (NoMethodError) fyi,ruby 版本:2.0.0。你是说 expand_path 吗?
  • 对不起,应该是expand_path。我会解决的。
猜你喜欢
  • 2012-10-17
  • 1970-01-01
  • 2015-02-23
  • 2012-12-03
  • 2012-04-21
  • 1970-01-01
  • 2018-03-26
  • 2015-07-15
  • 2012-01-11
相关资源
最近更新 更多