Modules
A simple mixin with include
Section titled “A simple mixin with include”module SomeMixin def foo puts "foo!" endend
class Bar include SomeMixin def baz puts "baz!" endend
b = Bar.newb.baz # => "baz!"b.foo # => "foo!"# works thanks to the mixinNow Bar is a mix of its own methods and the methods from SomeMixin.
Note that how a mixin is used in a class depends on how it is added:
- the
includekeyword evaluates the module code in the class context (eg. method definitions will be methods on instances of the class), extendwill evaluate the module code in the context of the singleton class of the object (methods are available directly on the extended object).
Modules and Class Composition
Section titled “Modules and Class Composition”You can use Modules to build more complex classes through composition. The include ModuleName directive incorporates a module’s methods into a class.
module Foo def foo_method puts 'foo_method called!' endend
module Bar def bar_method puts 'bar_method called!' endend
class Baz include Foo include Bar
def baz_method puts 'baz_method called!' endendBaz now contains methods from both Foo and Bar in addition to its own methods.
new_baz = Baz.newnew_baz.baz_method #=> 'baz_method called!'new_baz.bar_method #=> 'bar_method called!'new_baz.foo_method #=> 'foo_method called!'Module as Namespace
Section titled “Module as Namespace”Modules can contain other modules and classes:
module Namespace
module Child
class Foo; end
end # module Child
# Foo can now be accessed as: # Child::Foo
end # module Namespace
# Foo must now be accessed as:#Namespace::Child::FooA simple mixin with extend
Section titled “A simple mixin with extend”A mixin is just a module that can be added (mixed in) to a class. one way to do it is with the extend method. The extend method adds methods of the mixin as class methods.
module SomeMixin def foo puts "foo!" endend
class Bar extend SomeMixin def baz puts "baz!" endend
b = Bar.newb.baz # => "baz!"b.foo # NoMethodError, as the method was NOT added to the instanceBar.foo # => "foo!"# works only on the class itselfSyntax
Section titled “Syntax”module Name;
any ruby expressions;
endRemarks
Section titled “Remarks”Module names in Ruby are constants, so they have to start with a capital letter.
module foo; end # Syntax error: class/module name must be CONSTANT