Skip to content

Commit 629e65d

Browse files
authored
Simplify command method definition (#559)
* Remove unnecessary command argument generation code Now that all the supported Ruby versions handle splat args and splat kwargs, we don't need that args generation code anymore. * Remove unused command definition code If we look at `@EXTEND_COMMANDS`, all commands are defined in a file, which means the `if load_file` branch is always executed. Therefore we can drop the else branch of that condition. * Avoid defining unnecessary command methods There's no need to define another command method just to call `Command.execute`.
1 parent f68e891 commit 629e65d

File tree

1 file changed

+7
-28
lines changed

1 file changed

+7
-28
lines changed

lib/irb/extend-command.rb

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def self.install_extend_commands
246246
#
247247
# The optional +load_file+ parameter will be required within the method
248248
# definition.
249-
def self.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases)
249+
def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
250250
case cmd_class
251251
when Symbol
252252
cmd_class = cmd_class.id2name
@@ -255,33 +255,12 @@ def self.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases)
255255
cmd_class = cmd_class.name
256256
end
257257

258-
if load_file
259-
line = __LINE__; eval %[
260-
def #{cmd_name}(*opts, **kwargs, &b)
261-
Kernel.require_relative "#{load_file}"
262-
arity = ::IRB::ExtendCommand::#{cmd_class}.instance_method(:execute).arity
263-
args = (1..(arity < 0 ? ~arity : arity)).map {|i| "arg" + i.to_s }
264-
args << "*opts, **kwargs" if arity < 0
265-
args << "&block"
266-
args = args.join(", ")
267-
line = __LINE__; eval %[
268-
unless singleton_class.class_variable_defined?(:@@#{cmd_name}_)
269-
singleton_class.class_variable_set(:@@#{cmd_name}_, true)
270-
def self.#{cmd_name}_(\#{args})
271-
::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, \#{args})
272-
end
273-
end
274-
], nil, __FILE__, line
275-
__send__ :#{cmd_name}_, *opts, **kwargs, &b
276-
end
277-
], nil, __FILE__, line
278-
else
279-
line = __LINE__; eval %[
280-
def #{cmd_name}(*opts, &b)
281-
::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
282-
end
283-
], nil, __FILE__, line
284-
end
258+
line = __LINE__; eval %[
259+
def #{cmd_name}(*opts, **kwargs, &b)
260+
Kernel.require_relative "#{load_file}"
261+
::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
262+
end
263+
], nil, __FILE__, line
285264

286265
for ali, flag in aliases
287266
@ALIASES.push [ali, cmd_name, flag]

0 commit comments

Comments
 (0)