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
How to Organize Minitest/Unit Tests
Rails 4 User Roles and Permissions
How to Escape Strings for Terminal in Ruby
How Would I Go About Programmatically Interacting with VSt(I) Plugins to Synthesize Audio
How to Use Ruby Metaprogramming to Add Callbacks to a Rails Model
Why Do You Need "Require 'Bundler/Setup'"
What's the Difference Between Ruby Core API and Standard Library API
Single Table Inheritance or Class Table Inheritance
Ruby: How to Group a Ruby Array
How to Expire a View Cached Fragment from Console
What Are the Advantages of Mocha Over Rspec's Built in Mocking Framework
How to Authorize a Google Service Account Without the Default Credentials File
How to Remove Repeated Spaces in a String
Ruby Array of Hash. Group_By and Modify in One Line
Ruby Model Output Id as Object Oid
Backspace and Arrow Keys Aren't Working in Irb(Git Bash Console) on Windows MAChine