在 IRB 中重新加载 rubygem
有没有办法“重新加载”或“刷新”内存中的 rubygem?当我在 irb 中玩时,偶尔我喜欢修改我的 gem 文件,如果我需要相同的 gem,它不会更新到内存中并给出输出“false”。目前我必须退出IRB,返回IRB,然后再次需要gem,必须有更好的方法......它是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所建议的,您可以使用 Kernel#load。但是,不要浪费时间查找和加载每个 gem 文件,因为所需的所有文件都存储在 $" 中。有了这些知识,这里有一个重新加载 irb 命令:
例如,如果您在 irb 中使用 hirb gem ,你只需重新加载:
如果由于某种原因加载不起作用(它对文件扩展名比 require 更挑剔),你可以通过首先删除 $" 中的条目来重新请求任何文件。有了这个建议,上面的命令将是:
选择适合你的。就我个人而言,我使用后者。
As others have suggested, you can use Kernel#load. However, don't waste your time finding and loading each gem file as all files that have been required are stored in $". Armed with this knowledge, here's a reload irb command:
For example, if you were using the hirb gem in irb, you would simply reload with:
If for whatever reason load doesn't work (it is pickier about file extensions than require is), you can re-require any file by first deleting its entry in $". With this advice the above command would be:
Pick whichever works for you. Personally, I use the latter.
加载一个“filename.rb” ...
为每个更改一次
load 'filename.rb'
one at a time for each that was changed...
一种方法是使用
Kernel#load
。您可以像Kernel#require
一样使用它,只不过您必须显式包含文件扩展名。但是,它也有其缺点(即使像class Foo 这样简单的东西也会失败,因为不可能更改类的超类),所以我也想知道更好的方法来做到这一点。
在 Rails 中,您可以通过脚本/控制台上的
reload!
来实现此目的,但我不知道他们使用什么样的魔法。这个要点看起来很有希望,但我还没有测试过。
One way is to use
Kernel#load
. You can use it pretty much likeKernel#require
, except that you have to explicitly include the file extension. However, it has its drawbacks (even something as simple asclass Foo < Struct.new(:foo); end
fails, as it is not possible to change the superclass of an class), so I would also like to know a better way to do this.In rails you can achieve this with
reload!
on script/console, but I don't know what kind of magic they use.This gist looks promising but I haven't tested it.