rubyruby-1.9irb

How do I see all the methods/ properties in Object Class


I have started learning Rails 3.0 and decided to spend sometime learning Pure Ruby 1.9 and wonder if there is a way to View the Object Class since it is always available. Like is there a command that you can type in IRB for example that will display all the methods in Object Class like to_a, to_s, etc.. Thanks for any help


Solution

  • #methods is the method you want. It simply returns an array of symbols, which are all the names of the methods that object responds to.

    Object.new.methods
    

    Or more readable in irb:

    puts Object.new.methods.sort.to_yaml
    

    Or for the class methods:

    Object.methods
    

    One caveat though, some objects allow methods that won't be listed here. Anything implemented with a hook into #method_missing won't show up. This includes a lot of ActiveRecord methods as well as other rails objects.

    But so long as nothing tricky is going on, this is the list you seem to want.