Listando y Comprobando Métodos

Object defines methods for listing the names of methods defined on the object. These methods return arrays of methods names.

o = "a string" 
o.methods                   # => [ names of all public methods ] 
o.public_methods            # => the same thing
o.public_methods(false)     # Exclude inherited methods
o.protected_methods         # => []: there aren't any
o.private_methods           # => array of all private methods
o.private_methods(false)    # Exclude inherited private methods 
def o.single; 1; end        # Define a singleton method
o.singleton_methods         # => [:single]
It is also possible to query a class for the methods it defines rather than querying an instance of the class:
String.instance_methods == "s".public_methods # => true 
String.instance_methods(false) == "s".public_methods(false) # => true 
String.public_instance_methods == String.instance_methods # => true 
String.protected_instance_methods # => [] 
String.private_instance_methods(false) # => ["initialize_copy",
                                       #     "initialize"]



Casiano Rodriguez León 2015-01-07