Ruby Tutorial
Ruby CGI
Ruby Advanced
A module in Ruby is a collection of methods, constants, and other module declarations that can be mixed into classes using the include
keyword. Modules are a great way to share reusable code across multiple classes. Unlike classes, modules cannot be instantiated or subclassed and they don't have instances.
Here is a basic tutorial on how to define and use modules in Ruby:
1. Defining a Module:
Here is the basic syntax for defining a module:
module Greeter def greet(name) puts "Hello, #{name}!" end end
In this example, Greeter
is the module name, and it has a method greet
.
2. Including a Module in a Class:
You can include a module in a class using the include
keyword. The methods in the module become available as instance methods in the class:
class Person include Greeter end alice = Person.new alice.greet("Alice") # Outputs: Hello, Alice!
In this example, Person
is a class that includes the Greeter
module. alice
is an instance of Person
, and it can call the greet
method defined in the Greeter
module.
3. Module as a Namespace:
Modules can also be used as a namespace to prevent method name clashes when mixing in multiple modules:
module Math PI = 3.14159265359 def self.square(number) number * number end end puts Math::PI # Outputs: 3.14159265359 puts Math.square(3) # Outputs: 9
In this example, Math
is a module that defines a constant PI
and a method square
. You can access these with the ::
operator (e.g., Math::PI
and Math.square
).
4. Module Mixins:
One of the most powerful aspects of Ruby modules is the ability to share functionality between unrelated classes through mixins. When you include a module in a class, Ruby effectively inserts those module methods into the class, allowing instances of the class to use the methods as though they were defined right in the class.
Remember that while classes in Ruby can only inherit from a single superclass, they can include as many modules as they like, allowing for a form of multiple inheritance. This makes modules a very powerful tool for sharing behavior in an organized and reusable way.
Defining modules in Ruby: Modules in Ruby are containers for methods, constants, and classes.
module MyModule def my_method puts "This is a method in MyModule" end end
Including modules in Ruby classes: Modules can be included in classes to share behavior.
module MyModule def my_method puts "This is a method in MyModule" end end class MyClass include MyModule end
Ruby module methods: Modules can have methods that act as utility functions.
module MathModule def square(x) x * x end end
Mixins in Ruby modules: Mixins are modules that are included in classes to provide additional behavior.
module MyMixin def shared_method puts "This is a shared method" end end class MyClass include MyMixin end
Namespacing with Ruby modules: Modules can be used for namespacing to avoid naming conflicts.
module MyNamespace class MyClass # ... end end
Module constants in Ruby: Constants defined in a module are accessible within that module.
module MyModule MY_CONSTANT = 42 end
Module instance variables in Ruby: Modules can have instance variables that are shared among classes including the module.
module MyModule @shared_variable = 10 def show_variable puts @shared_variable end end
Ruby extend module:
The extend
keyword is used to add module methods to a single object.
module MyModule def my_method puts "This is a method in MyModule" end end obj = Object.new obj.extend(MyModule) obj.my_method
Nested modules in Ruby: Modules can be nested to create hierarchical namespaces.
module OuterModule module InnerModule # ... end end
Module inheritance in Ruby: Modules can inherit from other modules to share behavior.
module BaseModule def base_method puts "This is a method in BaseModule" end end module ExtendedModule extend BaseModule end