Ruby:如何“要求"当前工作目录中的文件? [英] Ruby: how to "require" a file from the current working dir?

查看:105
本文介绍了Ruby:如何“要求"当前工作目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从当前文件夹中获取文件?

How do I require a file from the current folder?

我有一个名为sql_parser.rb的文件,其中包含一个类.我想将此文件也包含在同一文件夹的另一个文件中,所以我使用了:

I have a file called sql_parser.rb that contains a class. I want to include this in another file also in the same folder, so I used:

require 'sql_parser'

从该文件夹运行时失败:

That fails when I run from that folder:

LoadError: no such file to load -- sql_parser

我尝试在该文件所在的文件夹中使用IRB,并从那里要求它,但是存在相同的问题.

I tried using IRB in the folder where this file exists and requiring it from there, but had the same issue.

推荐答案

在ruby 1.9.x中,可以使用方法require_relative.请参阅 http://www.ruby-doc. org/core-1.9.3/Kernel.html#method-i-require_relative .

In ruby 1.9.x, you can use the method require_relative. See http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require_relative.

这篇关于Ruby:如何“要求"当前工作目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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