Ruby has a strong reflection system. This is useful when you are debugging. The more you know, the easier debugging will be. But if you are starting, the following ones will make your life better.
my_object.inspect
This will give you a string representation of the object. Very useful when programming and debugging.
my_object.respond_to?(:method_name)
This one will tell you if the object can respond to a method. So Let's say that you want to call batman.swing
. But can you? You can query the object with batman.respond_to?(:swing)
and it will answer with true or false. Notice how you need to use the colon for the method name when sent as a parameter.
my_object.methods
(my_object.methods - Object.new.methods).sort
The first one will list all the methods that the object can respond to. Depending on the object, the list can be large. Once you look through them, you will discover that many of the methods were not defined in the class itself. The last version will remove all of the methods that are inherited by Object, the root parent object that most object descend from.
my_object.method(:method_name).source_location
This is perhaps the most useful one when looking for code. This incantation will give you the source location for the method that the object responds to.