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

  • inspiration

    _why: "when you don't create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create."

  • exceptions

    This bug makes me a little sad. From one of the patches for fixing: - @string = (str.string rescue str) + @string = str.respond_to?(:string) ?…

  • minimal openid consumer

    Here's a minimal OpenID consumer using the Ruby OpenID libraries. (Their included examples only contain Rails spew.) This example is useful for…

  • 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

  • inspiration

    _why: "when you don't create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create."

  • exceptions

    This bug makes me a little sad. From one of the patches for fixing: - @string = (str.string rescue str) + @string = str.respond_to?(:string) ?…

  • minimal openid consumer

    Here's a minimal OpenID consumer using the Ruby OpenID libraries. (Their included examples only contain Rails spew.) This example is useful for…