A 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 <code>Module#module_function</code>) In the descriptions that follow, the parameter <i>syml</i> refers to a symbol, which is either a quoted string or a <code>Symbol</code> (such as <code>:name</code>). module Mod include Math CONST = 1 def meth
…
end end Mod.class #=> Module Mod.constants #=> ["E", "PI", "CONST"] Mod.instance_methods #=> ["meth"]
| Public Methods | |
|---|---|
| < | Returns true if mod is a subclass of other. Returns nil if there’s no relationship between the two. (Think of the relationship in terms of the class definition: "class A<B" implies "A<B"). |
| <= | Returns true if mod is a subclass of other or is the same as other. Returns nil if there’s no relationship between the two. (Think of the relationship in terms of the class definition: "class A<B" implies "A<B"). |
| <=> | Comparison—Returns -1 if mod includes other_mod, 0 if mod is the same as other_mod, and +1 if mod is included by other_mod or if mod has no relationship with other_mod. Returns nil if other_mod is not a module. |
| == | Equality—At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning. |
| === | 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. |
| > | Returns true if mod is an ancestor of other. Returns nil if there’s no relationship between the two. (Think of the relationship in terms of the class definition: "class A<B" implies "B>A"). |
| >= | Returns true if mod is an ancestor of other, or the two modules are the same. Returns nil if there’s no relationship between the two. (Think of the relationship in terms of the class definition: "class A<B" implies "B>A"). |
| alias_ |
Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden. |
| ancestors | Returns a list of modules included in mod (including mod itself). |
| append_ |
When this module is included in another, Ruby calls append_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and module variables of this module to mod if this module has not already been added to mod or one of its ancestors. See also Module#include. |
| attr | Defines a named attribute for this module, where the name is symbol.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_ |
Equivalent to calling ``attrsymbol, true’’ on each symbol in turn. |
| attr_ |
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_ |
Creates an accessor method to allow assignment to the attribute aSymbol.id2name. |
| autoload | Registers filename to be loaded (using Kernel::require) the first time that name (which may be a String or a symbol) is accessed in the namespace of mod. |
| autoload? | Returns filename to be loaded if name is registered as autoload in the namespace of mod. |
| class_ |
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. The optional filename and lineno parameters set the text for error messages. |
| class_ |
Returns true if the given class variable is defined in obj. |
| class_ |
Returns the value of the given class variable (or throws a NameError exception). The @@ part of the variable name should be included for regular class variables |
| class_ |
Sets the class variable names by symbol to object. |
| class_ |
Returns an array of the names of class variables in mod and the ancestors of mod. |
| const_ |
Returns true if a constant with the given name is defined by mod. |
| const_ |
Returns the value of the named constant in mod. |
| const_ |
Invoked when a reference is made to an undefined constant in mod. It is passed a symbol for the undefined constant, and returns a value to be used for that constant. The following code is a (very bad) example: if reference is made to an undefined constant, it attempts to load a file whose name is the lowercase version of the constant (thus class Fred is assumed to be in file fred.rb). If found, it returns the value of the loaded class. It therefore implements a perverse kind of autoload facility. |
| const_ |
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 | Returns an array of the names of all constants defined in the system. This list includes the names of all modules and classes. |
| constants | 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). |
| define_ |
Defines an instance method in the receiver. The method |
| extend_ |
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. |
| extended | Not documented |
| freeze | Prevents further modifications to mod. |
| include | Invokes Module.append_features on each parameter in turn. |
| include? | Returns true if module is included in mod or one of mod’s ancestors. |
| included | Callback invoked whenever the receiver is included in another module or class. This should be used in preference to Module.append_features if your code wants to perform some action when a module is included in another. |
| included_ |
Returns the list of modules included in mod. |
| instance_ |
Returns an UnboundMethod representing the given instance method in mod. |
| instance_ |
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_ |
Not documented |
| method_ |
Returns true if the named method is defined by mod (or its included modules and, if mod is a class, its ancestors). Public and protected methods are matched. |
| method_ |
Not documented |
| method_ |
Not documented |
| module_ |
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. The optional filename and lineno parameters set the text for error messages. |
| module_ |
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. |
| name | Returns the name of the module mod. |
| nesting | Returns the list of Modules nested at the point of call. |
| new | Creates a new anonymous module. If a block is given, it is passed the module object, and the block is evaluated in the context of this module using module_eval. |
| private | With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility. |
| private_ |
Makes existing class methods private. Often used to hide the default constructor new. |
| private_ |
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. |
| private_ |
Returns true if the named private method is defined by _ mod_ (or its included modules and, if mod is a class, its ancestors). |
| protected | With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility. |
| protected_ |
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. |
| protected_ |
Returns true if the named protected method is defined by mod (or its included modules and, if mod is a class, its ancestors). |
| public | With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility. |
| public_ |
Makes a list of existing class methods public. |
| public_ |
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. |
| public_ |
Returns true if the named public method is defined by mod (or its included modules and, if mod is a class, its ancestors). |
| remove_ |
Removes the definition of the sym, returning that constant’s value. |
| remove_ |
Removes the definition of the given constant, returning that constant’s value. Predefined classes and singleton objects (such as true) cannot be removed. |
| remove_ |
Removes the method identified by symbol from the current class. For an example, see Module.undef_method. |
| to_ |
Return a string representing this module or class. For basic classes and modules, this is the name. For singletons, we show information on the thing we’re attached to as well. |
| undef_ |
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. |
<code/>and<pre/>for code samples.