Ruby要求'file'不起作用,但要求'.​​/file'起作用.为什么? [英] Ruby require 'file' doesn't work but require './file' does. Why?

查看:132
本文介绍了Ruby要求'file'不起作用,但要求'.​​/file'起作用.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满红宝石文件的文件夹,当我尝试使用require 'file'在同一目录中的另一个文件中需要一个文件时,我得到一个LoadError,但是当我使用require './file'时,一切正常.有人可以向我解释为什么会发生这种情况吗?是否可以在没有添加./的情况下以某种方式要求文件?

I have a folder full of ruby files, and when I try and require one file in another that is in the same directory using require 'file' I get a LoadError but when I use require './file' everything works fine. Can somebody explain to me why this happens and if there is any way I can require a file without adding a ./ onto the file?

(目录图片):

推荐答案

如果要不是从系统$LOAD_PATH而是相对于文件目录来require的文件,则正在require定义开始,您应该使用 require_relative . (如您所见,没有完全广泛记录.)

If you want to require a file not from the system $LOAD_PATH but rather relative to the directory of the file you are requireing from, you should use require_relative. (Which, as you can see, isn't exactly extensively documented.)

这篇关于Ruby要求'file'不起作用,但要求'.​​/file'起作用.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆