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?

Schneems
  • 14,918
  • 9
  • 57
  • 84
  • if I was to code this functionality in where should it go? Rubygems? Is that community generally accepting of new feature pull requests? – Schneems Aug 13 '10 at 17:44

3 Answers3

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.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
cldwalker
  • 6,155
  • 2
  • 27
  • 19
  • 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"' – picardo Jan 08 '11 at 02:57
  • 2
    It doesn't, no. The regex is too restrictive - you have to specify the full path. I modified my version, replacing ^ with /. – Hakanai May 10 '13 at 02:04
8

load 'filename.rb'

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

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
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.

sluukkonen
  • 2,566
  • 1
  • 19
  • 12
  • `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. – tadman Aug 12 '10 at 01:15