lib/irb/extend-command.rb in irb-1.6.3 vs lib/irb/extend-command.rb in irb-1.6.4
- old
+ new
@@ -244,47 +244,25 @@
#
# Will also define any given +aliases+ for the method.
#
# The optional +load_file+ parameter will be required within the method
# definition.
- def self.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases)
+ def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
case cmd_class
when Symbol
cmd_class = cmd_class.id2name
when String
when Class
cmd_class = cmd_class.name
end
- if load_file
- kwargs = ", **kwargs" if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0"
- line = __LINE__; eval %[
- def #{cmd_name}(*opts#{kwargs}, &b)
- Kernel.require_relative "#{load_file}"
- arity = ::IRB::ExtendCommand::#{cmd_class}.instance_method(:execute).arity
- args = (1..(arity < 0 ? ~arity : arity)).map {|i| "arg" + i.to_s }
- args << "*opts#{kwargs}" if arity < 0
- args << "&block"
- args = args.join(", ")
- line = __LINE__; eval %[
- unless singleton_class.class_variable_defined?(:@@#{cmd_name}_)
- singleton_class.class_variable_set(:@@#{cmd_name}_, true)
- def self.#{cmd_name}_(\#{args})
- ::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, \#{args})
- end
- end
- ], nil, __FILE__, line
- __send__ :#{cmd_name}_, *opts#{kwargs}, &b
- end
- ], nil, __FILE__, line
- else
- line = __LINE__; eval %[
- def #{cmd_name}(*opts, &b)
- ::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
- end
- ], nil, __FILE__, line
- end
+ line = __LINE__; eval %[
+ def #{cmd_name}(*opts, **kwargs, &b)
+ Kernel.require_relative "#{load_file}"
+ ::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
+ end
+ ], nil, __FILE__, line
for ali, flag in aliases
@ALIASES.push [ali, cmd_name, flag]
end
end
@@ -368,61 +346,7 @@
end
], __FILE__, line
end
CE.install_extend_commands
- end
-
- # A convenience module for extending Ruby methods.
- module MethodExtender
- # Extends the given +base_method+ with a prefix call to the given
- # +extend_method+.
- def def_pre_proc(base_method, extend_method)
- base_method = base_method.to_s
- extend_method = extend_method.to_s
-
- alias_name = new_alias_name(base_method)
- module_eval %[
- alias_method alias_name, base_method
- def #{base_method}(*opts)
- __send__ :#{extend_method}, *opts
- __send__ :#{alias_name}, *opts
- end
- ]
- end
-
- # Extends the given +base_method+ with a postfix call to the given
- # +extend_method+.
- def def_post_proc(base_method, extend_method)
- base_method = base_method.to_s
- extend_method = extend_method.to_s
-
- alias_name = new_alias_name(base_method)
- module_eval %[
- alias_method alias_name, base_method
- def #{base_method}(*opts)
- __send__ :#{alias_name}, *opts
- __send__ :#{extend_method}, *opts
- end
- ]
- end
-
- # Returns a unique method name to use as an alias for the given +name+.
- #
- # Usually returns <code>#{prefix}#{name}#{postfix}<num></code>, example:
- #
- # new_alias_name('foo') #=> __alias_of__foo__
- # def bar; end
- # new_alias_name('bar') #=> __alias_of__bar__2
- def new_alias_name(name, prefix = "__alias_of__", postfix = "__")
- base_name = "#{prefix}#{name}#{postfix}"
- all_methods = instance_methods(true) + private_instance_methods(true)
- same_methods = all_methods.grep(/^#{Regexp.quote(base_name)}[0-9]*$/)
- return base_name if same_methods.empty?
- no = same_methods.size
- while !same_methods.include?(alias_name = base_name + no)
- no += 1
- end
- alias_name
- end
end
end