This evening I was wanting to look up some methods on ActiveRecord::Base. We all know that a lot of the methods are not Rdoc’d so I usually have to dive into the source to see what’s going on. While not too difficult, I was wanting a way to look up methods while I’m in script/console. So I wrote a little Ruby script to lookup methods. Throw this script in your lib directory or add the ml method to one of your existing utility classes.
class Utils
def self.ml c,m
c.methods.each { |n|
if (n.index"#{m.to_s}")
puts n
end
};nil
end
end
Here’s a sample run from when I was trying to remember all the find methods on AR::Base:
shiny:~/rails/blah damon$ script/console
Loading development environment.
>> Utils.ml ActiveRecord::Base, :find
validate_find_options
find
find_all
deprecated_find_in_collection_method
deprecated_find_all_in_collection_method
find_on_conditions
find_first
after_find
validate_find_options_without_deleted
validate_find_options_with_deleted
find_by_sql
=> nil
>>
Pretty useful.
Thanks to Dave Thomas for tweaking.
UPDATE: New tips have come in from readers Mike Clark and Robert Rasmussen.
Mike informs me about the joy that is ~/.irbrc and Robert suggests that I add the method to Object. Both of these suggestions make it even easier to look up a method.
I’ve also tweaked the code a little so you can get a list of all methods if you pass in no params.
Add these lines to your ~/.irbrc
class Object
def ml(*m)
self.methods.each { |n|
if (m.empty?)
puts n
else
if (n.index"#{m[0].to_s}");puts n;end;
end
};nil
end
end
Now, you can just use the ml method on any object in console (or irb):
>> String.ml :inc
included_modules
include?
=> nil
>> ActiveRecord::Base.ml :conn
connected?
firebird_connection
sqlite3_connection
postgresql_connection
connection_without_query_cache=
mysql_connection
sqlserver_connection
retrieve_connection
clear_connection_cache!
sqlite_connection
establish_connection
connection
connection=
remove_connection
active_connections
threaded_connections
threaded_connections=
=> nil
>> String.ml "?"
blank?
respond_to?
nil?
include?
equal?
method_defined?
eql?
public_method_defined?
autoload?
instance_of?
private_method_defined?
tainted?
kind_of?
protected_method_defined?
yaml_tag_subclasses?
is_a?
const_defined?
frozen?
=> nil
5 comments ↓
Here’s one that’s not feature-equivalent but is more succinct and also works well in the console. Accepts either a regex or a string. class Object def ml(m) methods.select{|n| n =~ /#{m}/} end end >> “Hello World”.ml “case” => [”upcase”, “upcase!”, “downcase”, “downcase!”, “casecmp”, “swapcase”, “swapcase!”] >> “Hello World”.ml /c.se/ => [”upcase”, “upcase!”, “downcase”, “downcase!”, “casecmp”, “swapcase”, “swapcase!”]
Cool! I love the succintness of that. Using grep() had been suggested by someone also, but I couldn’t seem to get it to work the same way in the method as it did in the console. Thanks!
Oh yea. Should have thought of that one: class Object def ml(m) methods.grep(/#{m}/) end end That works for me in the console.
Yep, your syntax /#{m}/ is the ticket that I was missing.
[…] will need a more flexible lookup mechanism without leaving your irb session. This is a recap of a prior post on the subject which allows you to search multiple keywords at […]
Leave a Comment