How to Reload a Script in Irb

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.

PRY or IRB - reload class and forget deleted functionality

You can use remove_const to remove the class from its parent, either from the Module it is in:

My::Module.send(:remove_const, :MyClass)

or from Object if it was not declared inside a Module:

Object.send(:remove_const, :MyClass)

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.

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.

How do I drop to the IRB prompt from a running script?

you can use ruby-debug to get access to irb

require 'rubygems'
require 'ruby-debug'
x = 23
puts "welcome"
debugger
puts "end"

when program reaches debugger you will get access to irb.



Related Topics



Leave a reply



Submit