Modules and Mixins
In addition to classes, Ruby also has modules. A module is like a class, but it cannot be instantiated like a class. A class can include a module so that when the class is instantiated, it gets the included module’s methods and so forth. (The include method comes from the Module class: http://www.
ruby-doc.org/core/classes/Module.html.) The methods from an included module become instance methods in the class that includes the module. This is called mixing in, and a module is referred to as a mixin. You can include more than one module (which is similar to multiple inheritance), but
you can only inherit from one class (single inheritance).
Because identifiers are overridden by the last definition of the identifier (e.g., for methods or constants), this scheme avoids name collision. A module is a form of a namespace in Ruby.
A namespace is a set of names—such as method names—that have a scope or context. A Ruby class can also be considered a namespace.
A Ruby module associates a single name with a set of method and constant names. The module name can be used in classes or in other modules. Generally, the scope or context of such a namespace is the class or module where the namespace (module name) is included.
A module name must be a constant; that is, it must start with an uppercase letter. A module can contain methods, constants, other modules, and even classes. It can inherit from another module, but it may not inherit from a class. As a class may include a module, it may also include modules that have inherited other modules. Here’s an example:
module Dice
# virtual roll of a pair of dice
def roll
r_1 = rand(6); r_2 = rand(6)
r1 = r_1>0?r_1:1; r2 = r_2>0?r_2:6
total = r1+r2
printf( "You rolled %d and %d (%d).\n", r1, r2, total )
total
end
end
class Game
include Dice
end
g = Game.new
g.roll
If the module Dice and the class Game were in separate files, just require the file containing the module before including the module. The file containing the Dice module might look like this:
module Dice
# virtual roll of a pair of dice
def roll
r_1 = rand(6); r_2 = rand(6)
r1 = r_1>0?r_1:1; r2 = r_2>0?r_2:6
total = r1+r2
printf( "You rolled %d and %d (%d).\n", r1, r2, total )
total
end
end
And the file containing the Game class might look like this:
#!/usr/bin/env ruby
require 'dice'
class Game
include Dice
end
g = Game.new
g.roll
In addition to classes, Ruby also has modules. A module is like a class, but it cannot be instantiated like a class. A class can include a module so that when the class is instantiated, it gets the included module’s methods and so forth. (The include method comes from the Module class: http://www.
ruby-doc.org/core/classes/Module.html.) The methods from an included module become instance methods in the class that includes the module. This is called mixing in, and a module is referred to as a mixin. You can include more than one module (which is similar to multiple inheritance), but
you can only inherit from one class (single inheritance).
Because identifiers are overridden by the last definition of the identifier (e.g., for methods or constants), this scheme avoids name collision. A module is a form of a namespace in Ruby.
A namespace is a set of names—such as method names—that have a scope or context. A Ruby class can also be considered a namespace.
A Ruby module associates a single name with a set of method and constant names. The module name can be used in classes or in other modules. Generally, the scope or context of such a namespace is the class or module where the namespace (module name) is included.
A module name must be a constant; that is, it must start with an uppercase letter. A module can contain methods, constants, other modules, and even classes. It can inherit from another module, but it may not inherit from a class. As a class may include a module, it may also include modules that have inherited other modules. Here’s an example:
module Dice
# virtual roll of a pair of dice
def roll
r_1 = rand(6); r_2 = rand(6)
r1 = r_1>0?r_1:1; r2 = r_2>0?r_2:6
total = r1+r2
printf( "You rolled %d and %d (%d).\n", r1, r2, total )
total
end
end
class Game
include Dice
end
g = Game.new
g.roll
If the module Dice and the class Game were in separate files, just require the file containing the module before including the module. The file containing the Dice module might look like this:
module Dice
# virtual roll of a pair of dice
def roll
r_1 = rand(6); r_2 = rand(6)
r1 = r_1>0?r_1:1; r2 = r_2>0?r_2:6
total = r1+r2
printf( "You rolled %d and %d (%d).\n", r1, r2, total )
total
end
end
And the file containing the Game class might look like this:
#!/usr/bin/env ruby
require 'dice'
class Game
include Dice
end
g = Game.new
g.roll
No comments:
Post a Comment