Reload Rubygem in Irb

Reload rubygems in irb?

I did not try, but I think you might be looking for Gem.clear_paths

Reset the dir and path values. The next time dir or path is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.

Reload rubygem in IRB

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.

How can I reload a script in IRB?

In irb, File.expand_path(__FILE__)} will just return "#{path you ran irb from}/(irb)". Which creates a path that doesn't actually exist. Luckily all file paths are relative to where you ran irb anyway. This means all you need is:

load "lib/query.rb"

If you want to use the __FILE__ in an actual file, that's fine, but don't expect it to produce a valid path in irb. Because an irb there is no "file" at all, so it cannot return valid path at all.

Also, __FILE__ will work fine if used in a file loaded into irb via load or require. Cause that's kinda what it's for.

Ruby - create gem: reload console with updated gem content

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.

accessing variables in loaded source while in irb

like this:

def my_array
[1, 2, 3, 4, 5]
end

How to require for the second time

load does not require (hmm) a full path. It expects a complete filename with an extension.

p load 'date.rb' #=> true
p load 'date.rb' #=> true
p load 'date' #=> LoadError


Related Topics



Leave a reply



Submit