Skip to content

Conversation

@ahogappa
Copy link

@ahogappa ahogappa commented Jan 13, 2026

Summary

Add reload command to IRB that reloads files loaded via require, require_relative, or autoload during an IRB session.

Motivation

When developing gems and debugging/testing with IRB, it's tedious to restart IRB every time you modify a file. This feature allows you to reload modified files without restarting the session.

Implementation

Uses Ruby::Box's sandboxed $LOADED_FEATURES to detect which files are loaded by require. The basic flow is:

  1. When require is called from the IRB prompt, execute it inside a new Ruby::Box
  2. Compare $LOADED_FEATURES before and after to detect newly loaded files
  3. Track the detected Ruby files for later reloading
  4. Load the files into the main environment using load (native extensions use require)
  5. When reload command is executed, re-load all tracked files

Usage

Prerequisites

  • Ruby 4.0+ with RUBY_BOX=1 environment variable
  • IRB.conf[:RELOADABLE_REQUIRE] = true in your .irbrc

Example

# .irbrc
IRB.conf[:RELOADABLE_REQUIRE] = true

# In IRB session
irb> require 'mylib'
irb> MyLib.hello  # => "original"
# ... edit mylib.rb ...
irb> reload
Reloaded: /path/to/mylib.rb
irb> MyLib.hello  # => "modified"

Limitations

  • Constant redefinition warnings will appear on reload (uses load internally)
  • Native extensions (.so, .bundle) cannot be reloaded
  • Files loaded via Ruby::Box#require directly are not tracked
  • Context mode 5 (IRB running inside a Ruby::Box) is not supported
    • These could be implemented in future PRs if there's demand

Introduce ReloadableRequire module that enables reloading of files
loaded via require/require_relative in IRB sessions. This feature
requires Ruby::Box (Ruby 4.0+) and can be enabled with:

  IRB.conf[:RELOADABLE_REQUIRE] = true

The module tracks loaded Ruby files and allows them to be reloaded
while keeping native extensions loaded normally.
Add 'reload' command that reloads all files previously loaded via
require/require_relative in the IRB session. Shows appropriate error
messages when ReloadableRequire is not enabled or no files are loaded.
Add integration tests covering:
- require and require_relative with nested dependencies
- autoload with nested require
- reload command behavior
- $LOADED_FEATURES and $LOAD_PATH consistency
Set class became a core class in Ruby 3.2, but earlier versions
(2.7, 3.0, 3.1) require explicit 'require "set"' to use it.
This fixes NameError: uninitialized constant IRB::Set.

register(:cd, Command::CD)
register(:copy, Command::Copy)
register(:reload, Command::Reload)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command name reload might be confused with Rails console's reload!. Consider alternatives like reload_requires or refresh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant