Skip to content

Instantly share code, notes, and snippets.

@swarley
Created March 30, 2012 06:26
Show Gist options
  • Save swarley/2247617 to your computer and use it in GitHub Desktop.
Save swarley/2247617 to your computer and use it in GitHub Desktop.
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