我一直认为这类事情很丑陋:
require File.join(File.dirname(__FILE__), 'hirb/config')有没有更漂亮的替代方案,也许是为Rails编写的?
require_relative 'hirb/config'
require_relative '../another/file'发布于 2009-10-07 20:25:39
你可以使用extend the kernel。
module Kernel
def require_relative(path)
require File.join(File.dirname(caller[0]), path.to_str)
end
end发布于 2009-10-07 17:48:09
最好的方法可能是准备您的加载路径,因此您不需要执行所有这些操作。对于您的主模块或init文件来说,引入其他几个位置并不是特别困难。
这也受RUBYLIB环境变量以及-I命令行参数的影响。
$: << File.expand_path(File.join('..', 'lib'), File.dirname(__FILE__))发布于 2009-10-07 17:14:13
你可以这样做
Dir.chdir(File.dirname(__FILE__) do
require 'hirb/config'
require '../another/file'
end当然,这是否更好是一个品味问题。
https://stackoverflow.com/questions/1532934
复制相似问题