Method: Pry::Method.from_str

Defined in:
lib/pry/method.rb

.from_str(name, target = TOPLEVEL_BINDING, options = {}) ⇒ Pry::Method?

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.

Parameters:

  • name (String)

    The name of the method to retrieve.

  • target (Binding) (defaults to: TOPLEVEL_BINDING)

    The context in which to search for the method.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :instance (Boolean)

    Look for an instance method if name doesn’t contain any context.

  • :methods (Boolean)

    Look for a bound/singleton method if name doesn’t contain any context.

Returns:

  • (Pry::Method, nil)

    A Pry::Method instance containing the requested method, or nil if name is nil or no method could be located matching the parameters.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pry/method.rb', line 43

def from_str(name, target = TOPLEVEL_BINDING, options = {})
  if name.nil?
    nil
  elsif name.to_s =~ /(.+)\#(\S+)\Z/
    context = Regexp.last_match(1)
    meth_name = Regexp.last_match(2)
    from_module(target.eval(context), meth_name, target)
  elsif name.to_s =~ /(.+)(\[\])\Z/
    context = Regexp.last_match(1)
    meth_name = Regexp.last_match(2)
    from_obj(target.eval(context), meth_name, target)
  elsif name.to_s =~ /(.+)(\.|::)(\S+)\Z/
    context = Regexp.last_match(1)
    meth_name = Regexp.last_match(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) ||
      from_str(name, target, methods: true)
  end
rescue Pry::RescuableException
  nil
end