Module: IRB::MethodExtender (Ruby 2.5.7)
Description: Module : IRB::MethodExtender - Ruby 2.5.7

### Home Classes Methods

### In Files

- irb/extend-command.rb

### Methods

- #def_post_proc
- #def_pre_proc
- #new_alias_name

### Class/Module Index

Quicksearch

- IRB::Abort
- IRB::Context
- IRB::ContextExtender
- IRB::ExtendCommandBundle
- IRB::FileInputMethod
- IRB::Frame
- IRB::InputMethod
- IRB::Inspector
- IRB::Irb
- IRB::IrbLoader
- IRB::JobManager
- IRB::LoadAbort
- IRB::MethodExtender
- IRB::Notifier
- IRB::Notifier::AbstractNotifier
- IRB::Notifier::CompositeNotifier
- IRB::Notifier::LeveledNotifier
- IRB::Notifier::NoMsgNotifier
- IRB::OutputMethod
- IRB::ReadlineInputMethod
- IRB::StdioInputMethod
- IRB::StdioOutputMethod
- IRB::WorkSpace
- Object
- XMP
- XMP::StringInputMethod

No matching classes.

# IRB::MethodExtender

A convenience module for extending Ruby methods.

### Public Instance Methods

def_post_proc(base_method, extend_method) click to toggle source

Extends the given `base_method` with a postfix call to the given `extend_method`.

                   # File irb/extend-command.rb, line 273
    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) click to toggle source

Extends the given `base_method` with a prefix call to the given `extend_method`.

                   # File irb/extend-command.rb, line 257
    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 = "__") click to toggle source

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 bar; end
    new_alias_name('bar') #=> __alias_of__bar__2

                   # File irb/extend-command.rb, line 294
    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