Saturday, November 3, 2007

IRB: What Was That Method Again?

Ruby has a fantastically useful method on Object called #methods, which gives you a list of all the methods that object has.

But if you're dealing with an ActiveRecord model, or even, for that matter, a String, the number of methods in that list will huge - so huge that the list itself won't be much use to you.

The solution: grep_methods, which allows you to narrow your search down like so:

>> "any arbitrary string".grep_methods("ch")
=> ["chop!", "chop", "each_with_index", "chomp!", "match", "chomp", "each", "each_line", "each_byte"]

You can grab the code for it here. Put it in your .irbrc - it's very useful.