January 9th, 2008

  • evan

quick ruby tip: browsing methods

In part due to the bad docs, I often find myself digging through the method list on a Ruby object trying to remember what's available.

But obj.methods always produces a ton of output due to the methods available everywhere like instance_of? or nil?. Here's the tip: you can use set subtraction to cut out all the common ones. Suppose you're trying to recall the Exception API. Try subtracting the integer methods to see just the Exception-specific ones:
> Exception.new.methods - 1.methods
=> ["to_str", "set_backtrace", "exception", "message", "backtrace"]

Ever since I realized I could do this I use it all the time.

Update: our local Ruby expert describes a better way of doing this in the comments.