|
|||
Previous < |
Contents ^
|
Next >
|
![]() |
class Module |
|
Module
is a collection of methods and constants. The methods
in a module may be instance methods or module methods. Instance
methods appear as methods in a class when the module is included,
module methods do not. Conversely, module methods may be called
without creating an encapsulating object, while instance methods may
not. See
Module#module_function
on page 346.
In the descriptions that follow, the
parameter aSymbol refers to a symbol, which is either a
quoted string or a Symbol
(such as :name
).
module Mod
|
||
include Math
|
||
CONST = 1
|
||
def meth
|
||
# ...
|
||
end
|
||
end
|
||
Mod.type
|
� |
Module
|
Mod.constants
|
� |
["CONST", "E", "PI"]
|
Mod.instance_methods
|
� |
["meth"]
|
class methods | ||||||||||||||||||||||
constants | Module.constants -> anArray | |||||||||||||||||||||
Returns an array of the names of all constants defined in the
system. This list includes the names of all modules and classes.
|
||||||||||||||||||||||
nesting | Module.nesting -> anArray | |||||||||||||||||||||
Returns the list of Modules nested at the point of call.
|
||||||||||||||||||||||
new | Module.new -> aModule | |||||||||||||||||||||
Creates a new anonymous module. |
instance methods | |||||||||||||||||||||||||||||||||||||||||||||||||
<, <=, >, >= |
mod
relop
aModule
-> true or false
|
||||||||||||||||||||||||||||||||||||||||||||||||
Hierarchy Query---One module is considered greater than
another if it is included in (or is a
parent class of) the other module.
The other operators are
defined accordingly. If there is no relationship
between the modules, returns false for all operators.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
<=> | mod <=> aModule -> -1, 0, +1 | ||||||||||||||||||||||||||||||||||||||||||||||||
Comparison---Returns -1 if mod includes aModule, 0 if mod is the same as aModule, and +1 if mod is included by aModule or if mod has no relationship with aModule. | |||||||||||||||||||||||||||||||||||||||||||||||||
=== |
mod === anObject
-> true or false
|
||||||||||||||||||||||||||||||||||||||||||||||||
Case Equality---Returns true if anObject is an instance of mod
or one of mod's descendents. Of limited use for modules, but
can be used in case statements to classify objects by class.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
ancestors | mod.ancestors -> anArray | ||||||||||||||||||||||||||||||||||||||||||||||||
Returns a list of modules included in mod (including mod
itself).
|
|||||||||||||||||||||||||||||||||||||||||||||||||
class_eval |
mod.class_eval( aString ) -> anObject
mod.class_eval {| | block } -> anObject |
||||||||||||||||||||||||||||||||||||||||||||||||
Synonym for Module.module_eval .
|
|||||||||||||||||||||||||||||||||||||||||||||||||
class_variables | mod.class_variables -> anArray | ||||||||||||||||||||||||||||||||||||||||||||||||
Returns an array of the names of class variables in
mod and the ancestors of mod.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
clone | mod.clone -> aModule | ||||||||||||||||||||||||||||||||||||||||||||||||
Creates a new copy of a module.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
const_defined? |
mod.const_defined?( aSymbol ) -> true or false
|
||||||||||||||||||||||||||||||||||||||||||||||||
Returns true if a constant with
the given name is defined by mod.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
const_get | mod.const_get( aSymbol ) -> anObject | ||||||||||||||||||||||||||||||||||||||||||||||||
Returns the value of the named constant in mod.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
const_set | mod.const_set( aSymbol, anObject ) -> anObject | ||||||||||||||||||||||||||||||||||||||||||||||||
Sets the named constant to the given object, returning that
object. Creates a new constant if no constant with the given
name previously existed.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
constants | mod.constants -> anArray | ||||||||||||||||||||||||||||||||||||||||||||||||
Returns an array of the names of the constants accessible in mod. This includes the names of constants in any included modules (example at start of section). | |||||||||||||||||||||||||||||||||||||||||||||||||
included_modules | mod.included_modules -> anArray | ||||||||||||||||||||||||||||||||||||||||||||||||
Returns the list of modules included in mod.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
instance_methods |
mod.instance_methods(
includeSuper=false ) -> anArray
|
||||||||||||||||||||||||||||||||||||||||||||||||
Returns an array containing the names of public instance methods
in the receiver. For a module, these are the public methods; for
a class, they are the instance (not singleton) methods. With no
argument, or with an argument that is
false , the instance methods in mod are returned,
otherwise the methods in mod and mod's superclasses
are returned.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
method_defined? |
mod.method_defined?( aSymbol ) -> true or false
|
||||||||||||||||||||||||||||||||||||||||||||||||
Returns true if the named method is defined by mod
(or its included modules and, if mod is a class, its
ancestors).
|
|||||||||||||||||||||||||||||||||||||||||||||||||
module_eval |
mod.module_eval( aString ) -> anObject
mod.module_eval {| | block } -> anObject |
||||||||||||||||||||||||||||||||||||||||||||||||
Evaluates the string or block in the context of mod. This can
be used to add methods to a class. module_eval returns
the result of evaluating its argument.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
name | mod.name -> aString | ||||||||||||||||||||||||||||||||||||||||||||||||
Returns the name of the module mod. | |||||||||||||||||||||||||||||||||||||||||||||||||
private_class_method |
mod.private_class_method( [
aSymbol
]+
)
-> nil
|
||||||||||||||||||||||||||||||||||||||||||||||||
Makes existing class methods private.
Often used to hide the
default constructor new .
|
|||||||||||||||||||||||||||||||||||||||||||||||||
private_instance_methods |
mod.private_instance_methods( includeSuper= false )
-> anArray
|
||||||||||||||||||||||||||||||||||||||||||||||||
Returns a list of the private instance methods defined in mod.
If the optional parameter is not false , the methods
of any ancestors are included.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
protected_instance_methods |
mod.protected_instance_methods( includeSuper= false )
-> anArray
|
||||||||||||||||||||||||||||||||||||||||||||||||
Returns a list of the protected instance methods defined in mod.
If the optional parameter is not false , the methods
of any ancestors are included.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
public_class_method |
mod.public_class_method( [
aSymbol
]+
)
-> nil
|
||||||||||||||||||||||||||||||||||||||||||||||||
Makes a list of existing class methods public. | |||||||||||||||||||||||||||||||||||||||||||||||||
public_instance_methods |
mod.public_instance_methods( includeSuper= false )
-> anArray
|
||||||||||||||||||||||||||||||||||||||||||||||||
Returns a list of the public instance methods defined in mod.
If the optional parameter is not false , the methods
of any ancestors are included.
|
private methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
alias_method | alias_method( newID, oldID ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Makes newID a new copy of the method
oldID. This can be used to retain access to methods that
are overridden.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
append_features | append_features( aModule ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
When this module is included in
another, Ruby calls append_features in this module,
passing it the receiving module in aModule. Ruby's default
implementation is to add the constants, methods, and module
variables of this module to aModule if this
module has not already been added to aModule or one of its
ancestors. See also
Module#include
on page 345.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
attr |
attr( aSymbol, writable=false )
-> nil
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defines a named attribute for this module, where the name is
aSymbol.
id2name , creating an instance variable
(@name ) and a corresponding access method to read it.
If the optional writable
argument is true , also creates a method called name= to
set the attribute.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
attr_accessor |
attr_accessor( [
aSymbol
]+
)
-> nil
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Equivalent to calling ``attr
aSymbol
, true '' on each
aSymbol in turn.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
attr_reader |
attr_reader( [
aSymbol
]+
)
-> nil
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creates instance variables and corresponding methods that
return the value of each instance variable.
Equivalent to calling ``attr
:name'' on each name
in turn.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
attr_writer |
attr_writer( [
aSymbol
]+
)
-> nil
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creates an accessor method to allow assignment to the attribute
aSymbol
.id2name .
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
extend_object | extend_object( anObject ) -> anObject | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Extends the specified object by adding this module's constants
and methods (which are added as singleton methods). This is the
callback method used by
Object#extend
.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
include | include( [ aModule ]+ ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Invokes Module.append_features
(documented on page 344)
on each parameter in turn.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
method_added | method_added( aSymbol ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Invoked as a callback whenever a method is added to the receiver.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
module_function | module_function( [ aSymbol ]* ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Creates module functions for the named methods. These
functions may be called with the module as a receiver, and also
become available as instance methods to classes that mix in the
module. Module functions are copies of the original, and so may
be changed independently. The instance-method versions are made
private. If used with no arguments, subsequently defined methods
become module functions.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private | private( [ aSymbol ]* ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
With no arguments, sets the default visibility for subsequently
defined methods to private.
With arguments, sets the named
methods to have private visibility. See Access Control
starting on page 233.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
protected | protected( [ aSymbol ]* ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility. See Access Control starting on page 233. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public | public( [ aSymbol ]* ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility. See Access Control starting on page 233. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
remove_const | remove_const( aSymbol ) -> anObject | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Removes the definition of the given constant, returning that constant's value. Predefined classes and singleton objects (such as true) cannot be removed. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
remove_method | remove_method( aSymbol ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Removes the method identified by
aSymbol
from the current class.
For an example, see Module.undef_method .
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
undef_method | undef_method( aSymbol ) -> mod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Prevents the current class from responding to calls to the named
method. Contrast this with
remove_method , which
deletes the method from the particular class; Ruby will still
search superclasses and mixed-in modules for a possible
receiver.
|
Previous < |
Contents ^
|
Next >
|