Skip to content

Instantly share code, notes, and snippets.

@sheharyarn
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheharyarn/10772579 to your computer and use it in GitHub Desktop.
Save sheharyarn/10772579 to your computer and use it in GitHub Desktop.
Search an Object's Methods in Ruby easily
# Create a method 'search_methods' for the Object Class
class Object
def search_methods(qry)
self.methods & self.methods.select { |m| m.to_s.include? qry.to_s }
end
end
# Now search methods for any Ruby Object
Array.search_methods 'enum' # => [:to_enum, :enum_for]
Player.last.search_methods :trust # => [:untrust, :untrusted?, :trust]
"hello".search_methods "to_" # => [:to_i, :to_f, :to_s, :to_str, :to_sym, :to_r, :to_c, :to_enum]
@sheharyarn
Copy link
Author

Used in my dotfiles as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment