Skip to content

Instantly share code, notes, and snippets.

@banister
Created July 12, 2013 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banister/7941d1a83987f2d39c9f to your computer and use it in GitHub Desktop.
Save banister/7941d1a83987f2d39c9f to your computer and use it in GitHub Desktop.
# 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.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment