Method: Pry::Method.from_binding

Defined in:
lib/pry/method.rb

.from_binding(binding) ⇒ Pry::Method?

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.

Parameters:

  • binding (Binding)

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pry/method.rb', line 77

def from_binding(binding)
  meth_name = binding.eval('::Kernel.__method__')
  if [:__script__, nil].include?(meth_name)
    nil
  else
    method =
      begin
        if Object === binding.eval('self') # rubocop:disable Style/CaseEquality
          new(
            Kernel.instance_method(:method)
              .bind(binding.eval("self"))
              .call(meth_name)
          )
        else
          str = 'class << self; self; end' \
                '.instance_method(::Kernel.__method__).bind(self)'
          new(binding.eval(str))
        end
      rescue NameError, NoMethodError # rubocop:disable Lint/ShadowedException
        Disowned.new(binding.eval('self'), meth_name.to_s)
      end

    if WeirdMethodLocator.weird_method?(method, binding)
      WeirdMethodLocator.new(method, binding).find_method || method
    else
      method
    end
  end
end