Ruby - 创建gem:使用更新的gem内容重新加载控制台 [英] Ruby - create gem: reload console with updated gem content
问题描述
根据这篇文章,我们可以测试通过将这些行添加到我们的rakefile中:
任务:console do
require'irb'
需要'irb / completion'
需要'my_gem'#你知道该怎么做。
ARGV.clear
IRB.start
end
除了每当对宝石进行更改时,我都需要 exit
并重新运行 rake console
以获取代码已更新。作为创建/调试工具真的不方便......
有没有办法编写一个自定义的方法,它可以用作真棒从Rails中重载!
方法?
bash脚本不会工作,因为第一个命令在Ruby控制台中,我宁愿有一个100%的红宝石解决方案。
谢谢!
可以使用 $ LOADED_FEATURES
全局查找您的gem组件并使用 load
命令重新加载它们(使用 require 不会工作,因为它会跳过Ruby已经处理过的项目):
task:console do
require'irb'
require'irb / completion'
require'my_gem'#你知道该怎么做。
def重新加载!
#这里也改变'my_gem':
files = $ LOADED_FEATURES.select {| feat | feat =〜/ \ / my_gem \ //}
files.each {| file |加载文件}
结束
ARGV.clear
IRB.start
结束
请注意,如果您要编写本机扩展,则必须将其排除,并且您需要编译步骤,并且如果它们发生更改,则无论如何都要退出/重新启动。 / p>
According to this article, we can test around our gem code by adding those lines to our rakefile:
task :console do
require 'irb'
require 'irb/completion'
require 'my_gem' # You know what to do.
ARGV.clear
IRB.start
end
It works really well, except that whenever a change is made to the gem, I need to exit
and rerun rake console
to get the code updated. It is really not convenient as a creation/debugging tool ...
Is there a way to write a custom method that would act as the awesome reload!
method from Rails?
A bash script won't work as the first command is in the Ruby console, and I'd rather have a 100% ruby solution.
Thanks!
You can use the $LOADED_FEATURES
global to find the components of your gem and re-load them using the load
command (using require
won't work, as it skips items that Ruby has already processed):
task :console do
require 'irb'
require 'irb/completion'
require 'my_gem' # You know what to do.
def reload!
# Change 'my_gem' here too:
files = $LOADED_FEATURES.select { |feat| feat =~ /\/my_gem\// }
files.each { |file| load file }
end
ARGV.clear
IRB.start
end
Note this will fail if you are writing native extensions, you'll have to exclude them, and you'll want a compile step and to exit/re-start anyway if they change.
这篇关于Ruby - 创建gem:使用更新的gem内容重新加载控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!