Module methods let you group reusable code that can be shared across different parts of your program.
Module methods in Ruby
module ModuleName def self.method_name # code here end end
Use self.method_name to define a method that belongs to the module itself.
You can call module methods directly with ModuleName.method_name.
say_hello and calls it directly.module Greetings def self.say_hello puts "Hello!" end end Greetings.say_hello
module MathHelpers def self.square(x) x * x end end result = MathHelpers.square(4) puts result
This program defines two module methods: one to greet a person by name and one to add two numbers. It then calls both methods and prints their results.
module Tools def self.greet(name) "Hello, #{name}!" end def self.add(a, b) a + b end end puts Tools.greet("Alice") puts Tools.add(5, 7)
Module methods are like static methods in other languages.
You cannot create instances of a module, so module methods are called on the module itself.
Modules help keep your code organized and avoid repeating the same code in multiple places.
Module methods are defined with self.method_name inside a module.
You call module methods directly on the module, like ModuleName.method_name.
Use modules to group reusable code that can be shared across your program.
Practice
Solution
Step 1: Understand module method definition
In Ruby, to make a method callable directly on a module, you define it withself.method_nameinside the module.Step 2: Compare options
Define the method withself.method_nameinside the module usesself.method_name, which is correct. Other options either omitselfor use invalid syntax.Final Answer:
Define the method with self.method_name inside the module -> Option CQuick Check:
Module methods use self.method_name = A [OK]
- Forgetting to add self before method name
- Defining methods outside the module
- Using invalid syntax like def module.method_name
greet defined in module Welcome?Solution
Step 1: Recall how to call module methods
Module methods are called using dot notation:ModuleName.method_name.Step 2: Check options
Welcome.greetis the correct syntax using dot notation.Welcome#greetincorrectly uses #.greet.Welcomereverses the order.Welcome.new.greetconfuses it with an instance method call.Final Answer:
Welcome.greet -> Option DQuick Check:
Call module methods with dot = D [OK]
- Using # instead of . to call module methods
- Trying to call method with invalid syntax
- Confusing instance and module method calls
module MathTools
def self.square(x)
x * x
end
end
puts MathTools.square(4)Solution
Step 1: Understand the method definition
The methodsquareis defined as a module method usingself.square. It returns the square of the input.Step 2: Calculate the output
CallingMathTools.square(4)returns 4 * 4 = 16, which is printed.Final Answer:
16 -> Option AQuick Check:
4 squared = 16 [OK]
- Forgetting self and causing undefined method error
- Confusing output with input
- Expecting method name as output
module Greetings
def greet
"Hello!"
end
end
puts Greetings.greetSolution
Step 1: Check method definition inside module
The methodgreetis defined withoutself., so it is an instance method, not a module method.Step 2: Understand method call
CallingGreetings.greettries to call a module method, but none exists, causing an error.Final Answer:
Method greet is not defined as a module method -> Option AQuick Check:
Missing self. means no module method [OK]
- Defining instance methods instead of module methods
- Calling module method without defining it
- Assuming parentheses are required in method call
double that returns twice the input number?module Calculator # Add your method here end
Solution
Step 1: Define a module method with self
To create a module method, define it withself.method_nameinside the module.Step 2: Write method to return twice the input
The method should take one argumentxand returnx * 2.Final Answer:
def self.double(x) x * 2 end -> Option BQuick Check:
Module method with self and correct logic = A [OK]
- Omitting self and creating instance method
- Missing method argument
- Defining method outside module
Ready to prove it?
Pass the test and this pattern moves unknown
