Reload rubygem in IRB

前端 未结 3 1016
轻奢々
轻奢々 2020-12-14 02:43

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 n

相关标签:
3条回答
  • load 'filename.rb'

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

    0 讨论(0)
  • 2020-12-14 02:50

    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.

    0 讨论(0)
  • 2020-12-14 03:04

    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.

    0 讨论(0)
提交回复
热议问题