A module is a collection of classes and methods, stored in a file and defining a namespace. A module is an instance of the Module class, the "Module" class is inherited by the class: "Class", whose instances are the class definitions.
A module in a file named "nomefile.rb" can be included in a Ruby program and executed with the "load" or the "require" statements.
To use a method or a constant defined into a module the method or the constant name must be prefixed with the module name as: "Math::sin(90.0) ; Math::PI" "::" being the "resolution" operator.
Modules can be nested.
A module is defined by the keyword "module" ; the name of a module has the first letter capitalized; the name is a constant associated with the module
module Nomemodulo # module definition class ... # definition of a class in the module end def ... # definition of a function in the module end end
Modules can be included into a class (mixins); if variables are defined into the module, module accessor functions have to be defined in the module to use the variables as attributes of instances of the class including the module.
The statement "include" mixes a module into the namespace of a class, if the module is in its own file, and not in the same file of the class, the file containing the module must be loaded with "load" or "require" before the inclusion in the class:
module Mod # a simple module def func # with a simple method "A function of Mod!" end end class Classe include Mod # mixed into a class end b=Classe.new # an instance b.respond_to? :func => true # responds to the module method b.respond_to? "func" => true b.func # => "A function of Mod!"
The extend method can insert a module into an instance, and the methods of the module become singleton methods of the instance:
module Mod def func "A function of Mod!" end end a =  a.extend(Mod) a.func # => "A function of Mod!" a.singleton_methods # => ["func"]
When, for an object, the freeze method is called, the object can't be extended with a module.