Evan Martin (evan) wrote in evan_tech,
Evan Martin
evan
evan_tech

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.
Tags: ruby
Subscribe
  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 17 comments