Ruby Module Functions

EditRocket provides the following information on Module functions in the Ruby source code builder.

alias_method alias_method(new_name, old_name) - 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_features append_features(mod) - 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 attr(symbol, writable=false) - 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_accessor attr_accessor(symbol, ...) - Equivalent to calling ``attrsymbol, true'' on each symbol in turn.

attr_reader attr_reader(symbol, ...) - 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(symbol, ...) - Creates an accessor method to allow assignment to the attribute aSymbol.id2name.

autoload?(name) - Returns filename to be loaded if name is registered as autoload in the namespace of mod.

autoload(name, filename) - 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.

class_eval(string [, filename [, lineno]]) - 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_variable_defined?(symbol) - Returns true if the given class variable is defined in obj.

class_variable_get(symbol) - 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_variable_set(symbol, obj) - Sets the class variable names by symbol to object.

class_variables - Returns an array of the names of class variables in mod and the ancestors of mod.

const_defined?(sym) - Returns true if a constant with the given name is defined by mod.

const_get(sym) - Returns the value of the named constant in mod.

const_missing(sym) - "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_set(sym, obj) - 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 all constants defined in the system. This list includes the names of all modules and classes.

define_method define_method(symbol, method) - Defines an instance method in the receiver. The method parameter can be a Proc or Method object. If a block is specified, it is used as the method body. This block is evaluated using instance_eval, a point that is tricky to demonstrate because define_method is private. (This is why we resort to the send hack in this example.)

extend_object extend_object(obj) - 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(p1) - Not documented

freeze - Prevents further modifications to mod.

include?(module) - Returns true if module is included in mod or one of mod's ancestors.

include include(module, ...) - Invokes Module.append_features on each parameter in turn.

included included( othermod ) - 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_modules - Returns the list of modules included in mod.

instance_method(symbol) - Returns an UnboundMethod representing the given instance method in mod.

instance_methods(include_super=true) - 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_added(p1) - Not documented

method_defined?(symbol) - 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_removed(p1) - Not documented

method_undefined(p1) - Not documented

module_eval mod.class_eval(string [, filename [, lineno]]) - 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_function module_function(symbol, ...) - 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 private => self - With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility.

private_class_method(symbol, ...) - Makes existing class methods private. Often used to hide the default constructor new.

private_instance_methods(include_super=true) - 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_method_defined?(symbol) - Returns true if the named private method is defined by _ mod_ (or its included modules and, if mod is a class, its ancestors).

protected protected => self - With no arguments, sets the default visibility for subsequently defined methods to protected. With arguments, sets the named methods to have protected visibility.

protected_instance_methods(include_super=true) - 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_method_defined?(symbol) - Returns true if the named protected method is defined by mod (or its included modules and, if mod is a class, its ancestors).

public public => self - With no arguments, sets the default visibility for subsequently defined methods to public. With arguments, sets the named methods to have public visibility.

public_class_method(symbol, ...) - Makes a list of existing class methods public.

public_instance_methods(include_super=true) - 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_method_defined?(symbol) - Returns true if the named public method is defined by mod (or its included modules and, if mod is a class, its ancestors).

remove_class_variable remove_class_variable(sym) - Removes the definition of the sym, returning that constant's value.

remove_const remove_const(sym) - 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(symbol) - Removes the method identified by symbol from the current class. For an example, see Module.undef_method.

to_s - 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_method undef_method(symbol) - 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.