Module: IRB::MethodExtender
- Defined in:
- lib/irb/extend-command.rb
Overview
A convenience module for extending Ruby methods.
Instance Method Summary collapse
-
#def_post_proc(base_method, extend_method) ⇒ Object
Extends the given
base_methodwith a postfix call to the givenextend_method. -
#def_pre_proc(base_method, extend_method) ⇒ Object
Extends the given
base_methodwith a prefix call to the givenextend_method. -
#new_alias_name(name, prefix = "__alias_of__", postfix = "__") ⇒ Object
Returns a unique method name to use as an alias for the given
name.
Instance Method Details
#def_post_proc(base_method, extend_method) ⇒ Object
Extends the given base_method with a postfix call to the given extend_method.
395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/irb/extend-command.rb', line 395 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 |
#def_pre_proc(base_method, extend_method) ⇒ Object
Extends the given base_method with a prefix call to the given extend_method.
379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/irb/extend-command.rb', line 379 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 |
#new_alias_name(name, prefix = "__alias_of__", postfix = "__") ⇒ Object
Returns a unique method name to use as an alias for the given name.
Usually returns #{prefix}#{name}#{postfix}<num>, example:
new_alias_name('foo') #=> __alias_of__foo__
def ; end
new_alias_name('bar') #=> __alias_of__bar__2
416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/irb/extend-command.rb', line 416 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 |