Skip to content

Add reload command to re-load files loaded via require#1157

Open
ahogappa wants to merge 4 commits intoruby:masterfrom
ahogappa:add-reloadable-require
Open

Add reload command to re-load files loaded via require#1157
ahogappa wants to merge 4 commits intoruby:masterfrom
ahogappa:add-reloadable-require

Conversation

@ahogappa
Copy link
Copy Markdown

@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


register(:cd, Command::CD)
register(:copy, Command::Copy)
register(:reload, Command::Reload)
Copy link
Copy Markdown
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.

@ahogappa ahogappa force-pushed the add-reloadable-require branch from 3013701 to fe7a510 Compare March 15, 2026 11:20
@ahogappa ahogappa force-pushed the add-reloadable-require branch from fe7a510 to 30f842f Compare March 26, 2026 23:35
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