Skip to content

Commit 35ca8b4

Browse files
committed
feat: add reload command for ReloadableRequire
1 parent 60d9ecc commit 35ca8b4

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

lib/irb/command/reload.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
module IRB
4+
# :stopdoc:
5+
6+
module Command
7+
class Reload < Base
8+
category "IRB"
9+
description "Reload files that were loaded via require in IRB session."
10+
11+
def execute(_arg)
12+
unless reloadable_require_available?
13+
warn "The reload command requires IRB.conf[:RELOADABLE_REQUIRE] = true and Ruby::Box (Ruby 4.0+) with RUBY_BOX=1 environment variable."
14+
return
15+
end
16+
17+
files = IRB.conf[:__RELOADABLE_FILES__]
18+
if files.empty?
19+
puts "No files to reload. Use require to load files first."
20+
return
21+
end
22+
23+
files.each { |path| reload_file(path) }
24+
end
25+
26+
private
27+
28+
def reloadable_require_available?
29+
IRB.conf[:RELOADABLE_REQUIRE] && defined?(Ruby::Box) && Ruby::Box.enabled?
30+
end
31+
32+
def reload_file(path)
33+
load path
34+
puts "Reloaded: #{path}"
35+
rescue LoadError => e
36+
warn "Failed to reload #{path}: #{e.message}"
37+
rescue SyntaxError => e
38+
warn "Syntax error in #{path}: #{e.message}"
39+
end
40+
end
41+
end
42+
43+
# :startdoc:
44+
end

lib/irb/default_commands.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
require_relative "command/measure"
2727
require_relative "command/next"
2828
require_relative "command/pushws"
29+
require_relative "command/reload"
2930
require_relative "command/show_doc"
3031
require_relative "command/show_source"
3132
require_relative "command/step"
@@ -252,6 +253,7 @@ def load_command(command)
252253

253254
register(:cd, Command::CD)
254255
register(:copy, Command::Copy)
256+
register(:reload, Command::Reload)
255257
end
256258

257259
ExtendCommand = Command

0 commit comments

Comments
 (0)