class Pry::Method

This class wraps the normal `Method` and `UnboundMethod` classes to provide extra functionality useful to Pry.

Public Class Methods

all_from_class(klass, include_super=true) click to toggle source

Get all of the instance methods of a `Class` or `Module` @param [Class,Module] klass @param [Boolean] include_super Whether to include methods from ancestors. @return [Array]

# File lib/pry/method.rb, line 140
def all_from_class(klass, include_super=true)
  all_from_common(klass, :instance_method, include_super)
end
all_from_obj(obj, include_super=true) click to toggle source

Get all of the methods on an `Object` @param [Object] obj @param [Boolean] include_super Whether to include methods from ancestors. @return [Array]

# File lib/pry/method.rb, line 148
def all_from_obj(obj, include_super=true)
  all_from_common(obj, :method, include_super)
end
from_binding(b) click to toggle source

Given a `Binding`, try to extract the `::Method` it originated from and use it to instantiate a `Pry::Method`. Return `nil` if this isn't possible.

@param [Binding] b @return [Pry::Method, nil]

# File lib/pry/method.rb, line 72
def from_binding(b)
  meth_name = b.eval('::Kernel.__method__')
  if [:__script__, nil].include?(meth_name)
    nil
  else
    method = begin
               if Object === b.eval('self')
                 new(Kernel.instance_method(:method).bind(b.eval("self")).call(meth_name))
               else
                 new(b.eval('class << self; self; end.instance_method(::Kernel.__method__).bind(self)'))
               end
             rescue NameError, NoMethodError
               Disowned.new(b.eval('self'), meth_name.to_s)
             end

    if WeirdMethodLocator.weird_method?(method, b)
      WeirdMethodLocator.new(method, b).get_method || method
    else
      method
    end
  end
end
from_class(klass, name, target=TOPLEVEL_BINDING) click to toggle source

Given a `Class` or `Module` and the name of a method, try to instantiate a `Pry::Method` containing the instance method of that name. Return `nil` if no such method exists.

@param [Class, Module] klass @param [String] name @param [Binding] target The binding where the method is looked up. @return [Pry::Method, nil]

# File lib/pry/method.rb, line 119
def from_class(klass, name, target=TOPLEVEL_BINDING)
  new(lookup_method_via_binding(klass, name, :instance_method, target)) rescue nil
end
Also aliased as: from_module
from_module(klass, name, target=TOPLEVEL_BINDING) click to toggle source
Alias for: from_class
from_obj(obj, name, target=TOPLEVEL_BINDING) click to toggle source

Given an object and the name of a method, try to instantiate a `Pry::Method` containing the method of that name bound to that object. Return `nil` if no such method exists.

@param [Object] obj @param [String] name @param [Binding] target The binding where the method is looked up. @return [Pry::Method, nil]

# File lib/pry/method.rb, line 132
def from_obj(obj, name, target=TOPLEVEL_BINDING)
  new(lookup_method_via_binding(obj, name, :method, target)) rescue nil
end
from_str(name, target=TOPLEVEL_BINDING, options={}) click to toggle source

Given a string representing a method name and optionally a binding to search in, find and return the requested method wrapped in a `Pry::Method` instance.

@param [String, nil] name The name of the method to retrieve, or `nil` to

delegate to `from_binding` instead.

@param [Binding] target The context in which to search for the method. @param [Hash] options @option options [Boolean] :instance Look for an instance method if `name` doesn't

contain any context.

@option options [Boolean] :methods Look for a bound/singleton method if `name` doesn't

contain any context.

@return [Pry::Method, nil] A `Pry::Method` instance containing the requested

method, or `nil` if no method could be located matching the parameters.
# File lib/pry/method.rb, line 43
def from_str(name, target=TOPLEVEL_BINDING, options={})
  if name.nil?
    from_binding(target)
  elsif name.to_s =~ /(.+)\#(\S+)\Z/
    context, meth_name = $1, $2
    from_module(target.eval(context), meth_name, target)
  elsif name.to_s =~ /(.+)(\.|::)(\S+)\Z/
    context, meth_name = $1, $3
    from_obj(target.eval(context), meth_name, target)
  elsif options[:instance]
    from_module(target.eval("self"), name, target)
  elsif options[:methods]
    from_obj(target.eval("self"), name, target)
  else
    from_str(name, target, :instance => true) or
      from_str(name, target, :methods => true)
  end

rescue Pry::RescuableException
  nil
end
instance_method_definition?(name, definition_line) click to toggle source
# File lib/pry/method.rb, line 184
def instance_method_definition?(name, definition_line)
  /^define_method\(?\s*[:\"\]#{name}|^def\s*#{name}/ =~ definition_line.strip
end
instance_resolution_order(klass) click to toggle source

Get every `Class` and `Module`, in order, that will be checked when looking for methods on instances of the given `Class` or `Module`. This does not treat singleton classes of classes specially. @param [Class, Module] klass @return [Array[Class, Module]]

# File lib/pry/method.rb, line 170
def instance_resolution_order(klass)
  # include klass in case it is a singleton class,
  ([klass] + klass.ancestors).uniq
end
lookup_method_via_binding(obj, method_name, method_type, target=TOPLEVEL_BINDING) click to toggle source

In order to support 2.0 Refinements we need to look up methods inside the relevant Binding. @param [Object] obj The owner/receiver of the method. @param [Symbol] method_name The name of the method. @param [Symbol] method_type The type of method: :method or :instance_method @param [Binding] target The binding where the method is looked up. @return [Method, UnboundMethod] The 'refined' method object.

# File lib/pry/method.rb, line 102
def lookup_method_via_binding(obj, method_name, method_type, target=TOPLEVEL_BINDING)
  Pry.current[:obj] = obj
  Pry.current[:name] = method_name
  receiver = obj.is_a?(Module) ? "Module" : "Kernel"
  target.eval("::#{receiver}.instance_method(:#{method_type}).bind(Pry.current[:obj]).call(Pry.current[:name])")
ensure
  Pry.current[:obj] = Pry.current[:name] = nil
end
method_definition?(name, definition_line) click to toggle source
# File lib/pry/method.rb, line 175
def method_definition?(name, definition_line)
  singleton_method_definition?(name, definition_line) ||
    instance_method_definition?(name, definition_line)
end
new(method, known_info={}) click to toggle source

A new instance of `Pry::Method` wrapping the given `::Method`, `UnboundMethod`, or `Proc`.

@param [::Method, UnboundMethod, Proc] method @param [Hash] known_info Can be used to pre-cache expensive to compute stuff. @return [Pry::Method]

# File lib/pry/method.rb, line 223
def initialize(method, known_info={})
  @method = method
  @visibility = known_info[:visibility]
end
resolution_order(obj) click to toggle source

Get every `Class` and `Module`, in order, that will be checked when looking for an instance method to call on this object. @param [Object] obj @return [Array[Class, Module]]

# File lib/pry/method.rb, line 156
def resolution_order(obj)
  if Class === obj
    singleton_class_resolution_order(obj) + instance_resolution_order(Class)
  else
    klass = singleton_class(obj) rescue obj.class
    instance_resolution_order(klass)
  end
end
singleton_method_definition?(name, definition_line) click to toggle source
# File lib/pry/method.rb, line 180
def singleton_method_definition?(name, definition_line)
  /^define_singleton_method\(?\s*[:\"\]#{name}|^def\s*self\.#{name}/ =~ definition_line.strip
end

Public Instance Methods

==(obj) click to toggle source

@return [Boolean]

# File lib/pry/method.rb, line 459
def ==(obj)
  if obj.is_a? Pry::Method
    obj == @method
  else
    @method == obj
  end
end
alias?() click to toggle source

@return [Boolean] Is the method definitely an alias?

# File lib/pry/method.rb, line 454
def alias?
  name != original_name
end
aliases() click to toggle source

@return [Array<String>] All known aliases for the method. @note On Ruby 1.8 this method always returns an empty Array for methods

implemented in C.
# File lib/pry/method.rb, line 438
def aliases
  owner = @method.owner
  # Avoid using `to_sym` on {Method#name}, which returns a `String`, because
  # it won't be garbage collected.
  name = @method.name

  alias_list = owner.instance_methods.combination(2).select do |pair|
    pair.include?(name) &&
      owner.instance_method(pair.first) == owner.instance_method(pair.last)
  end.flatten
  alias_list.delete(name)

  alias_list.map(&:to_s)
end
bound_method?() click to toggle source

@return [Boolean] Whether the method is bound.

# File lib/pry/method.rb, line 421
def bound_method?
  is_a?(::Method)
end
doc() click to toggle source

@return [String, nil] The documentation for the method, or `nil` if it's

unavailable.

@raise [CommandError] Raises when the method was defined in the REPL.

# File lib/pry/method.rb, line 296
def doc
  @doc ||= case source_type
    when :c
      info = pry_doc_info
      info.docstring if info
    when :ruby
      if rbx? && !pry_method?
        get_comment_content(core_doc)
      elsif pry_method?
        get_comment_content(doc_for_pry_method)
      else
        get_comment_content(@method.comment)
      end
    end
end
dynamically_defined?() click to toggle source

@return [Boolean] Was the method defined outside a source file?

# File lib/pry/method.rb, line 411
def dynamically_defined?
  !!(source_file and source_file =~ /(\(.*\))|<.*>/)
end
is_a?(klass) click to toggle source

@param [Class] klass @return [Boolean]

# File lib/pry/method.rb, line 469
def is_a?(klass)
  klass == Pry::Method or @method.is_a?(klass)
end
Also aliased as: kind_of?
kind_of?(klass) click to toggle source
Alias for: is_a?
method_missing(method_name, *args, &block) click to toggle source

Delegate any unknown calls to the wrapped method.

# File lib/pry/method.rb, line 481
def method_missing(method_name, *args, &block)
  @method.send(method_name, *args, &block)
end
name() click to toggle source

Get the name of the method as a String, regardless of the underlying #name type. @return [String]

# File lib/pry/method.rb, line 230
def name
  @method.name.to_s
end
name_with_owner() click to toggle source

Get the name of the method including the class on which it was defined. @example

method(:puts).method_name
=> "Kernel.puts"

@return [String]

# File lib/pry/method.rb, line 257
def name_with_owner
  "#{wrapped_owner.method_prefix}#{name}"
end
original_name() click to toggle source

@return [String, nil] The original name the method was defined under,

before any aliasing, or `nil` if it can't be determined.
# File lib/pry/method.rb, line 405
def original_name
  return nil if source_type != :ruby
  method_name_from_first_line(source.lines.first)
end
pry_method?() click to toggle source

@return [Boolean] Was the method defined within the Pry REPL?

# File lib/pry/method.rb, line 431
def pry_method?
  source_file == Pry.eval_path
end
respond_to?(method_name) click to toggle source

@param [String, Symbol] method_name @return [Boolean]

# File lib/pry/method.rb, line 476
def respond_to?(method_name)
  super or @method.respond_to?(method_name)
end
signature() click to toggle source

@return [String] A representation of the method's signature, including its

name and parameters. Optional and "rest" parameters are marked with `*`
and block parameters with `&`. If the parameter names are unavailable,
they're given numbered names instead.
Paraphrased from `awesome_print` gem.
# File lib/pry/method.rb, line 371
def signature
  if respond_to?(:parameters)
    args = parameters.inject([]) do |arr, (type, name)|
      name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
      arr << case type
             when :req   then name.to_s
             when :opt   then "#{name}=?"
             when :rest  then "*#{name}"
             when :block then "&#{name}"
             else '?'
             end
    end
  else
    args = (1..arity.abs).map { |i| "arg#{i}" }
    args[-1] = "*#{args[-1]}" if arity < 0
  end

  "#{name}(#{args.join(', ')})"
end
singleton_method?() click to toggle source

@return [Boolean] Whether the method is a singleton method.

# File lib/pry/method.rb, line 426
def singleton_method?
  wrapped_owner.singleton_class?
end
source() click to toggle source

@return [String, nil] The source code of the method, or `nil` if it's unavailable.

# File lib/pry/method.rb, line 262
def source
  @source ||= case source_type
              when :c
                info = pry_doc_info
                if info and info.source
                  code = strip_comments_from_c_code(info.source)
                end
              when :ruby
                # clone of MethodSource.source_helper that knows to use our
                # hacked version of source_location for rbx core methods, and
                # our input buffer for methods defined in (pry)
                file, line = *source_location
                raise SourceNotFoundError, "Could not locate source for #{name_with_owner}!" unless file

                begin
                  code = Pry::Code.from_file(file).expression_at(line)
                rescue SyntaxError => e
                  raise MethodSource::SourceNotFoundError.new(e.message)
                end
                strip_leading_whitespace(code)
              end
end
source?() click to toggle source

Can we get the source code for this method? @return [Boolean]

# File lib/pry/method.rb, line 287
def source?
  !!source
rescue MethodSource::SourceNotFoundError
  false
end
source_file() click to toggle source

@return [String, nil] The name of the file the method is defined in, or

`nil` if the filename is unavailable.
# File lib/pry/method.rb, line 329
def source_file
  if source_location.nil?
    if !rbx? and source_type == :c
      info = pry_doc_info
      info.file if info
    end
  else
    source_location.first
  end
end
source_line() click to toggle source

@return [Fixnum, nil] The line of code in `#source_file` which begins

the method's definition, or `nil` if that information is unavailable.
# File lib/pry/method.rb, line 342
def source_line
  source_location.nil? ? nil : source_location.last
end
source_location() click to toggle source
# File lib/pry/method.rb, line 318
def source_location
  if @method.source_location && rbx?
    file, line = @method.source_location
    [RbxPath.convert_path_to_full(file), line]
  else
    @method.source_location
  end
end
source_range() click to toggle source

@return [Range, nil] The range of lines in `#source_file` which contain

the method's definition, or `nil` if that information is unavailable.
# File lib/pry/method.rb, line 348
def source_range
  source_location.nil? ? nil : (source_line)..(source_line + source.lines.count - 1)
end
source_type() click to toggle source

@return [Symbol] The source type of the method. The options are

`:ruby` for Ruby methods or `:c` for methods written in C.
# File lib/pry/method.rb, line 314
def source_type
  source_location.nil? ? :c : :ruby
end
super(times=1) click to toggle source

@return [Pry::Method, nil] The wrapped method that is called when you

use "super" in the body of this method.
# File lib/pry/method.rb, line 393
def super(times=1)
  if UnboundMethod === @method
    sup = super_using_ancestors(Pry::Method.instance_resolution_order(owner), times)
  else
    sup = super_using_ancestors(Pry::Method.resolution_order(receiver), times)
    sup &&= sup.bind(receiver)
  end
  Pry::Method.new(sup) if sup
end
unbound_method?() click to toggle source

@return [Boolean] Whether the method is unbound.

# File lib/pry/method.rb, line 416
def unbound_method?
  is_a?(::UnboundMethod)
end
undefined?() click to toggle source

Is the method undefined? (aka `Disowned`) @return [Boolean] false

# File lib/pry/method.rb, line 248
def undefined?
  false
end
visibility() click to toggle source

@return [Symbol] The visibility of the method. May be `:public`,

`:protected`, or `:private`.
# File lib/pry/method.rb, line 354
def visibility
 @visibility ||= if owner.public_instance_methods.any? { |m| m.to_s == name }
                   :public
                 elsif owner.protected_instance_methods.any? { |m| m.to_s == name }
                   :protected
                 elsif owner.private_instance_methods.any? { |m| m.to_s == name }
                   :private
                 else
                   :none
                 end
end
wrapped() click to toggle source

Get underlying object wrapped by this Pry::Method instance @return [Method, UnboundMethod, Proc]

# File lib/pry/method.rb, line 242
def wrapped
  @method
end
wrapped_owner() click to toggle source

Get the owner of the method as a Pry::Module @return [Pry::Module]

# File lib/pry/method.rb, line 236
def wrapped_owner
  @wrapped_owner ||= Pry::WrappedModule.new(owner)
end