We can find out a bit more about the methods and constants in a
particular object. Instead of just checking to see whether the object
responds to a given message, we can ask for methods by access level,
we can ask for just singleton methods, and we can have a look at
the object's constants.
class Demo
|
private
|
def privMethod
|
end
|
protected
|
def protMethod
|
end
|
public
|
def pubMethod
|
end
|
|
def Demo.classMethod
|
end
|
|
CONST = 1.23
|
end
|
|
Demo.private_instance_methods
|
� |
["privMethod"]
|
Demo.protected_instance_methods
|
� |
["protMethod"]
|
Demo.public_instance_methods
|
� |
["pubMethod"]
|
Demo.singleton_methods
|
� |
["classMethod"]
|
Demo.constants - Demo.superclass.constants
|
� |
["CONST"]
|
Module.constants
returns
all the constants available via a module, including
constants from the module's superclasses. We're not interested in
those just at the moment, so we'll subtract them from our list.
Given a list of method names, we might now be tempted to try calling them.
Fortunately, that's easy with Ruby.