Skip to content

Commit 67eba54

Browse files
authored
Unroll extension method generation (#882)
* Unroll extension method generation Given we only have 2 remaining extension setter methods, both of which only take 1 argument and don't have any alias, the current method generation logic is overly complicated. This commit simplifies the method generation logic by simply defining the methods directly in the `IRB::Context` class. * Fix use_loader extension
1 parent 6751778 commit 67eba54

File tree

3 files changed

+14
-42
lines changed

3 files changed

+14
-42
lines changed

lib/irb/command.rb

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -315,44 +315,4 @@ def self.extend_object(obj)
315315

316316
install_extend_commands
317317
end
318-
319-
# Extends methods for the Context module
320-
module ContextExtender
321-
CE = ContextExtender # :nodoc:
322-
323-
@EXTEND_COMMANDS = [
324-
[:eval_history=, "ext/eval_history.rb"],
325-
[:use_loader=, "ext/use-loader.rb"],
326-
]
327-
328-
# Installs the default context extensions as irb commands:
329-
#
330-
# Context#eval_history=:: +irb/ext/history.rb+
331-
# Context#use_tracer=:: +irb/ext/tracer.rb+
332-
# Context#use_loader=:: +irb/ext/use-loader.rb+
333-
def self.install_extend_commands
334-
for args in @EXTEND_COMMANDS
335-
def_extend_command(*args)
336-
end
337-
end
338-
339-
# Evaluate the given +command+ from the given +load_file+ on the Context
340-
# module.
341-
#
342-
# Will also define any given +aliases+ for the method.
343-
def self.def_extend_command(cmd_name, load_file, *aliases)
344-
line = __LINE__; Context.module_eval %[
345-
def #{cmd_name}(*opts, &b)
346-
Context.module_eval {remove_method(:#{cmd_name})}
347-
require_relative "#{load_file}"
348-
__send__ :#{cmd_name}, *opts, &b
349-
end
350-
for ali in aliases
351-
alias_method ali, cmd_name
352-
end
353-
], __FILE__, line
354-
end
355-
356-
CE.install_extend_commands
357-
end
358318
end

lib/irb/context.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ def use_tracer=(val)
166166
IRB.conf[:USE_TRACER] = val
167167
end
168168

169+
def eval_history=(val)
170+
self.class.remove_method(__method__)
171+
require_relative "ext/eval_history"
172+
__send__(__method__, val)
173+
end
174+
175+
def use_loader=(val)
176+
self.class.remove_method(__method__)
177+
require_relative "ext/use-loader"
178+
__send__(__method__, val)
179+
end
180+
169181
private def build_completor
170182
completor_type = IRB.conf[:COMPLETOR]
171183
case completor_type

lib/irb/ext/use-loader.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
55
#
66

7-
require_relative "../cmd/load"
7+
require_relative "../command/load"
88
require_relative "loader"
99

1010
class Object
@@ -49,7 +49,7 @@ def use_loader=(opt)
4949
if IRB.conf[:USE_LOADER] != opt
5050
IRB.conf[:USE_LOADER] = opt
5151
if opt
52-
if !$".include?("irb/cmd/load")
52+
if !$".include?("irb/command/load")
5353
end
5454
(class<<@workspace.main;self;end).instance_eval {
5555
alias_method :load, :irb_load

0 commit comments

Comments
 (0)