Viewing the available methods for a given object

Public, Ruby General


Created: Dec 10, 2014     Updated: Dec 10, 2014

As soon as an object comes into existence, it already responds to a number of messages. Every object is "born" with certain innate abilities. For example, if you have a class called Dog, and you create a new object like this, from the command line:

d =

You can see a list of the object's innate methods by calling the methods method. You can also throw in a sort operation, to make it easier to browse visually:

puts d.methods.sort

The result is a list of all the messages (methods) this newly minted object comes bundled with.


You can determine in advance (before you ask the object to do something) whether the object knows how to handle the message you want to send it, by using the respond_to? method. For example, to determine if the slice method can be called on the new object, type:

d.respond_to?('slice')   # => true