如何从 IRB 运行 .rb 文件? [英] How to run a .rb file from IRB?
问题描述
我从 Ruby on Rails 开始.我目前正在学习一个教程,它说我必须从 IRB 运行一个 .rb 文件,这将在我的当前目录中创建一个 .xml 文件.
I am starting out with Ruby on Rails. I am currently going through a tutorial where it says that I have to run a .rb file from IRB and that that will create a .xml file in my current directory.
我的问题是如何在 IRB 中运行 .rb 文件?
当我在 IRB 中运行这个 .rb 文件时,我是否必须在它所在的目录中?
My question is how do I run a .rb file in IRB?
And do I have to be in the directory where this .rb file lives when I run it in IRB?
我尝试了以下操作:只需在文件目录的命令行上输入 irb
.据我所知,这开始了 IRB 会议.
然后我输入了 irb "filename.rb"
它通过但没有在当前目录中创建任何东西,但至少它没有给出任何错误.
I tried the following: just typing irb
on the command line in the directory of the file. That starts an IRB session as far as I understand.
Then I typed irb "filename.rb"
which went through but didn't create anything in the current directory but at least it didn't give any errors.
我还尝试了一大堆其他东西,但这些东西显然给我带来了错误.所以我不认为我自己可以解决这个问题,谷歌搜索这个问题根本没有帮助.
I also tried a whole bunch of other stuff that plain gave me errors. So I don't think I can solve this myself and googling the matter didn't help at all.
我正在运行 Leopard.
I am running Leopard.
推荐答案
您可以在 irb 中通过要求或加载文件来运行"它.
You can "run" a file in irb by just requiring or loading it.
$ irb
>> load './filename.rb'
要在 irb 中更改当前工作目录,您可以使用 FileUtils:
To change your current working directory within irb, you can use FileUtils:
>> require 'fileutils'
>> FileUtils.pwd # prints working directory
>> FileUtils.cd '/path/to/somewhere' # changes the directory
这篇关于如何从 IRB 运行 .rb 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!