Created
March 30, 2012 06:26
-
-
Save swarley/2247617 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pry::Commands.create_command "find" do | |
description "Recursively search a Module/Class for a method : find [REGEX] [MODULE/CLASS]" | |
def process | |
regex = eval "/#{args[0..-2].join(' ')}/" | |
base = ::Class.const_get args.last | |
ret = search(regex, base) | |
output.puts ret.flatten | |
end | |
private | |
def search(regex, klass, current=[]) | |
return unless klass.is_a? Class | |
return if current.include? klass | |
current << klass | |
meths = [] | |
klass.methods.each do |x| | |
if x.to_s =~ regex | |
meths << "#{klass}##{x}" | |
end | |
end | |
klass.constants.each do |x| | |
meths += ((res = search(regex, klass.const_get(x), current)) ? res : []).flatten | |
end | |
return meths | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment