Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

ruby on rails - Reload rubygem in IRB

Is there a way to "reload" or "refresh" a rubygem in memory? As i'm playing in irb, occasionally I like to modify my gem files, and if i require the same gem, it does not update into memory and gives the output "false". Currently I have to exit IRB, get back into IRB and then require the gem again, there has to be a better way...what is it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

 def reload(require_regex)
  $".grep(/^#{require_regex}/).each {|e| load(e) }
 end

For example, if you were using the hirb gem in irb, you would simply reload with:

>> reload 'hirb'

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:

 def reload(require_regex)
   $".grep(/^#{require_regex}/).each {|e| $".delete(e) && require(e) }
 end

Pick whichever works for you. Personally, I use the latter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...