您可以在 irb 会话中自动在每个命令上“要求"ruby 文件吗? [英] Can you 'require' ruby file in irb session, automatically, on every command?
问题描述
我目前正在编辑一个文件,我正在使用 irb 来测试 api:
I am currently editing a file, and I'm using irb to test the api:
> require './file.rb'
> o = Object.new
> o.method
然后我希望能够编辑 file.rb,并能够立即看到更改.示例:假设 new_method 在我第一次需要 file.rb 时不存在:
I then want to be able to edit the file.rb, and be able to see changes immediately. Example: assume new_method did not exist when I first required file.rb:
> o.new_method
这将返回一个错误.是否有沙箱/开发人员模式或方法,我可以在无需每次重新加载文件的情况下实现上述目标?无论如何,在第一个 require 之后 Require 将不起作用.我假设最坏的情况是我不得不使用负载.
Which will return an error. Is there a sandbox/developer mode or a method whereby I can achieve the above without having to reload the file every time? Require will not work after the first require, regardless. I assume worst case I'd have to use load instead.
推荐答案
我通常创建一个这样的简单函数:
I usually create a simple function like this:
def reload
load 'myscript.rb'
# Load any other necessary files here ...
end
这样,一个简单的reload
将重新导入我正在处理的所有脚本.这不是自动的,但这是我能想到的最接近的事情.
With that, a simple reload
will re-import all of the scripts that I'm working on. It's not automatic, but it's the closest thing that I've been able to come up with.
当使用不存在的方法调用您的对象时,您可以覆盖 method_missing
以自动调用此函数.不过我自己从来没有尝试过,所以我不能给出任何具体的建议.如果您正在调用一个已经存在但只是被修改过的方法,这也无济于事.
You may be able to override method_missing
to call this function automatically when your object is invoked with a method that doesn't exist. I've never tried it myself, though, so I can't give any specific advice. It also wouldn't help if you're calling a method that already exists but has simply been modified.
由于我自己的懒惰,我已经将鼠标上的一个可编程按钮映射到按键序列reloadirb
时,只需动动小指即可重新加载所有内容.因此,当我不使用irb
时,我最终会无意中将字符串reload"插入到文档中(但这完全是一个不同的问题).
In my own laziness, I've gone as far as mapping one of the programmable buttons on my mouse to the key sequence "reload<enter>". When I'm using irb
, all it takes is the twitch of a pinky finger to reload everything. Consequently when I'm not using irb
, I end up with the string "reload" inserted in documents unintentionally (but that's a different problem entirely).
这篇关于您可以在 irb 会话中自动在每个命令上“要求"ruby 文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!