Reload rubygem in IRB
Asked Answered
B

3

20

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?

Bernadine answered 11/8, 2010 at 21:58 Comment(1)
if I was to code this functionality in where should it go? Rubygems? Is that community generally accepting of new feature pull requests?Bernadine
J
18

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.

Jerome answered 12/8, 2010 at 7:38 Comment(2)
Does this also work for scripts included from the filesystem? It didn't work for mine...and my require statement looks like this: 'require "#{File.expand_path(FILE)}/../lib/query"'Schopenhauerism
It doesn't, no. The regex is too restrictive - you have to specify the full path. I modified my version, replacing ^ with /.Avictor
D
8

load 'filename.rb'

one at a time for each that was changed...

Devitalize answered 11/8, 2010 at 22:1 Comment(0)
F
6

One way is to use Kernel#load. You can use it pretty much like Kernel#require, except that you have to explicitly include the file extension. However, it has its drawbacks (even something as simple as class 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.

Freiburg answered 11/8, 2010 at 22:34 Comment(1)
reload! is only supposed to load things in the app/ directory, nothing more. There are limitations to this, too, which is why a lot of your classes and controllers appear as anonymous classes in your development environment so they can be disposed of completely and re-initialized on each request.Emaemaciate

© 2022 - 2024 — McMap. All rights reserved.