Ruby:如何将文件加载到交互式 ruby 控制台 (IRB)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13112245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby: How to load a file into interactive ruby console (IRB)?
提问by nbonbon
I am using IRB (interactive ruby console) to learn how to program with Ruby. How do I load a file into the console if I write my programs in a text editor first?
我正在使用 IRB(交互式 ruby 控制台)来学习如何使用 Ruby 进行编程。如果我先在文本编辑器中编写程序,如何将文件加载到控制台?
回答by PhilG
If you only need to load one file into IRB you can invoke it with irb -r ./your_file.rbif it is in the same directory.
如果您只需要将一个文件加载到 IRB 中,您可以使用irb -r ./your_file.rb它在同一目录中调用它。
This automatically requires the file and allows you to work with it immediately.
这会自动需要该文件并允许您立即使用它。
回答by wachr
Using ruby 1.9.3 on Ubuntu 14.04, I am able to load files from the current directory into irbwith the following command line:
在 Ubuntu 14.04 上使用 ruby 1.9.3,我可以irb使用以下命令行从当前目录加载文件:
irb -I . -r foo.rb
where foo.rbis the file I want to load from my current directory. The -Ioption is necessary to add the current directory (.) to ruby's load path, as explained in the ruby man page. This makes it possible to requirefiles from the current directory, which is what the -roption to irbaccomplishes.
foo.rb我想从当前目录加载的文件在哪里。该-I选项是将当前目录 ( .)添加到 ruby 的加载路径所必需的,如 ruby 手册页中所述。这使得require从当前目录中获取文件成为可能,这就是该-r选项要irb完成的任务。
The key piece that wasn't obvious for me when I had this problem is the -Ioption. Once you do that, you can call require 'foo.rb'from within irbfor any files in the current directory. And of course, you can specify any directory you want, not just .with the -Ioption. To include multiple directories on the load path, separate them with a colon (:), e.g.:
当我遇到这个问题时,对我来说并不明显的关键部分是-I选项。完成此操作后,您可以require 'foo.rb'从内部调用irb当前目录中的任何文件。当然,您可以指定任何您想要的目录,而不仅仅是.使用-I选项。要在加载路径上包含多个目录,请用冒号 (:) 将它们分开,例如:
irb -I foo/:bar/:baz/
This command will add the directories foo, bar, and bazto ruby's load path.
此命令将添加目录foo、bar和baz到 ruby 的加载路径。
The final alternative is to use the relative or absolute path to the file when using requireor -rto load a file:
最后一种选择是在使用require或-r加载文件时使用文件的相对或绝对路径:
irb -r ./foo.rb
or from within irb:
或从内部irb:
> require './foo.rb'
回答by okysabeni
Type in irb
输入 irb
And then
进而
require './ruby_file.rb'
This is assuming that ruby_file.rb is in the same directory. Adjust accordingly.
这是假设 ruby_file.rb 在同一目录中。相应调整。
回答by Assad Ebrahim
Two ways:
两种方式:
- to load source withoutrunning the program -- this gives access to all variables and functions:
- 在不运行程序的情况下加载源- 这可以访问所有变量和函数:
source("filename.rb")
source("filename.rb")
- to run program and then drop into interactive mode -- this only gives access to functions, not variables:
- 运行程序然后进入交互模式——这只能访问函数,而不是变量:
require("filename.rb")
require("filename.rb")
回答by Michael
It depends on your ruby. Ruby 1.8 includes your current path, while ruby 1.9 does not. Evaluate $:to determine if your path is included or not. So in ruby 1.9 you must use the entire path, which is always a safe bet.
这取决于你的红宝石。Ruby 1.8 包含您当前的路径,而 ruby 1.9 不包含。评估$:以确定是否包含您的路径。所以在 ruby 1.9 中你必须使用整个路径,这总是一个安全的赌注。
Then you can use requireor loadto include the file.
然后您可以使用require或load来包含该文件。
requiredoes not require you to add the suffix of the file when trying to find it and will only include the file once. requireshould be used instead of loadmost of the time.
require不要求您在尝试查找文件时添加文件后缀,并且只会包含该文件一次。 require应该使用而不是load大部分时间。
Check out Adding a directory to $LOAD_PATH (Ruby)if you are going to be using ruby 1.8
如果您打算使用 ruby 1.8,请查看将目录添加到 $LOAD_PATH (Ruby)
回答by vinoth
Type the ruby codes in the text editor
在文本编辑器中输入 ruby 代码
Save it with the extension .rb(for example: demo.rb).
使用扩展名.rb保存它(例如:demo.rb)。
In linux, open your terminal then change directory to the current location of that file (cd command is used to change directory).
在 linux 中,打开终端,然后将目录更改为该文件的当前位置(cd 命令用于更改目录)。
After that,type irb and your filename(don't forget to include your extension(.rb)).
之后,输入 irb 和您的文件名(不要忘记包括您的扩展名(.rb))。
In that image,I loaded a simple ruby file which only prints "ruby".
在该图像中,我加载了一个仅打印“ruby”的简单 ruby 文件。
回答by tagaism
For those, who want to load .rb file from the different directory. Just add a string representer of the directory to $: variable.
对于那些想要从不同目录加载 .rb 文件的人。只需将目录的字符串表示添加到 $: 变量。
> $: << "/directory/to/the/required/rb/file"
> require "some_file"
回答by Mike Kennedy
Another way to load the path into irb is just type require then drag and drop the file into the terminal. -tested using Linux Mint.
将路径加载到 irb 的另一种方法是键入 require 然后将文件拖放到终端中。- 使用 Linux Mint 测试。


