class Pry::Command::FindMethod

Public Instance Methods

options(opt) click to toggle source
# File lib/pry/commands/find_method.rb, line 31
def options(opt)
  opt.on :n, :name,    "Search for a method by name"
  opt.on :c, :content, "Search for a method based on content in Regex form"
end
process() click to toggle source
# File lib/pry/commands/find_method.rb, line 36
def process
  return if args.empty?

  klass = search_class

  matches = opts.content? ? content_search(klass) : name_search(klass)
  show_search_results(matches)
end

Private Instance Methods

additional_info(header, method) click to toggle source

Return the matched lines of method source if `-c` is given or “” if `-c` was not given

# File lib/pry/commands/find_method.rb, line 100
def additional_info(header, method)
  if opts.content?
    ': ' + colorize_code(matched_method_lines(header, method))
  else
    ""
  end
end
matched_method_lines(header, method) click to toggle source
# File lib/pry/commands/find_method.rb, line 108
def matched_method_lines(header, method)
  method.source.split(/\n/).select { |x| x =~ pattern }.join(
    "\n#{' ' * header.length}"
  )
end
pattern() click to toggle source

@return [Regexp] The pattern to search for.

# File lib/pry/commands/find_method.rb, line 48
def pattern
  @pattern ||= ::Regexp.new args[0]
end
print_matches(matches) click to toggle source

pretty-print a list of matching methods.

@param [Array<Method>] matches

print_matches_for_class(klass, grouped) click to toggle source

Print matched methods for a class

recurse_namespace(klass, done = {}) { |klass| ... } click to toggle source

Run the given block against every constant in the provided namespace.

@param [Module] klass The namespace in which to start the search. @param [Hash<Module,Boolean>] done The namespaces we've already visited (private) @yieldparam klass Each class/module in the namespace.

# File lib/pry/commands/find_method.rb, line 120
def recurse_namespace(klass, done = {}, &block)
  return if !klass.is_a?(Module) || done[klass]

  done[klass] = true

  yield klass

  klass.constants.each do |name|
    next if klass.autoload?(name)

    begin
      const = klass.const_get(name)
    rescue RescuableException # rubocop:disable Lint/HandleExceptions
      # constant loading is an inexact science at the best of times,
      # this often happens when a constant was .autoload? but someone
      # tried to load it. It's now not .autoload? but will still raise
      # a NameError when you access it.
    else
      recurse_namespace(const, done, &block)
    end
  end
end
search_all_methods(namespace) { |method| ... } click to toggle source

Gather all the methods in a namespace that pass the given block.

@param [Module] namespace The namespace in which to search. @yieldparam [Method] method The method to test @yieldreturn [Boolean] @return [Array<Method>]

# File lib/pry/commands/find_method.rb, line 150
def search_all_methods(namespace)
  done = Hash.new { |h, k| h[k] = {} }
  matches = []

  recurse_namespace(namespace) do |klass|
    methods = Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)
    methods.each do |method|
      next if done[method.owner][method.name]

      done[method.owner][method.name] = true

      matches << method if yield method
    end
  end

  matches
end
search_class() click to toggle source

The class to search for methods. We only search classes, so if the search object is an instance, return its class. If no search object is given search `target_self`.

# File lib/pry/commands/find_method.rb, line 67
def search_class
  klass = if args[1]
            target.eval(args[1])
          else
            target_self
          end

  klass.is_a?(Module) ? klass : klass.class
end
show_search_results(matches) click to toggle source

Output the result of the search.

@param [Array] matches

# File lib/pry/commands/find_method.rb, line 55
def show_search_results(matches)
  if matches.empty?
    output.puts bold("No Methods Matched")
  else
    print_matches(matches)
  end
end