Bird
Raised Fist0
Rubyprogramming~10 mins

Module methods in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Module methods
Define Module
↓
Define Method inside Module
↓
Include or Extend Module in Class
↓
Call Module Method from Class or Module
↓
Execute Method Code
↓
Return Result
This flow shows how a method inside a module is defined, included or extended in a class, then called and executed.
Execution Sample
Ruby
module Greetings
  def hello
    "Hello from module!"
  end
end

class Person
  include Greetings
end

puts Person.new.hello
Defines a module with a method, includes it in a class, then calls the method on a class instance.
Execution Table
StepActionEvaluationResult
1Define module GreetingsModule createdModule Greetings exists with method hello
2Define method hello inside GreetingsMethod hello definedMethod hello returns string
3Define class PersonClass createdClass Person exists
4Include Greetings in PersonModule includedPerson instances have hello method
5Create Person instancePerson.new calledNew Person object created
6Call hello on Person instancehello method found in GreetingsReturns "Hello from module!"
7puts outputPrint stringHello from module!
8EndNo more codeProgram ends
💡 Program ends after printing the module method output
Variable Tracker
VariableStartAfter Step 5After Step 6Final
GreetingsModule definedUnchangedUnchangedUnchanged
PersonClass definedUnchangedUnchangedUnchanged
person_instanceUndefinedPerson object createdUnchangedUnchanged
outputUndefinedUndefined"Hello from module!""Hello from module!"
Key Moments - 2 Insights
Why can Person instances call the hello method even though it's defined in the module?
Because the module Greetings is included in Person (see step 4), its methods become available to Person instances (step 6).
What is the difference between include and extend for modules?
Include adds module methods as instance methods (like here), while extend adds them as class methods. This example uses include (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the module method hello actually called?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Check the 'Action' column for 'Call hello on Person instance' in step 6.
According to the variable tracker, what is the value of 'output' after step 6?
A"Hello from module!"
BUndefined
CPerson object
DModule Greetings
💡 Hint
Look at the 'output' row under 'After Step 6' in the variable tracker.
If we replaced 'include Greetings' with 'extend Greetings' in the class, what would happen?
APerson instances can still call hello method
BPerson class itself can call hello method, instances cannot
Chello method is not available anywhere
DProgram will error at runtime
💡 Hint
Recall the key moment about include vs extend in modules.
Concept Snapshot
Module methods are defined inside modules.
Use include to add module methods as instance methods.
Use extend to add module methods as class methods.
Call included methods on instances.
This helps share code across classes.
Full Transcript
This example shows how to define a method inside a Ruby module and use it in a class. First, the module Greetings is created with a method hello that returns a string. Then, the class Person is defined and includes the Greetings module. Including the module makes its methods available as instance methods of Person. When we create a new Person object and call hello on it, Ruby finds the method in the included module and executes it, returning the greeting string. Finally, puts prints the returned string. The execution table traces each step from defining the module and class, including the module, creating an instance, calling the method, and printing the output. The variable tracker shows how variables like the module, class, instance, and output change during execution. Key moments clarify why including a module allows instance method calls and the difference between include and extend. The quiz tests understanding of when the method is called, variable values, and the effect of using extend instead of include.

Practice

(1/5)
1. What is the correct way to define a method inside a Ruby module so it can be called directly on the module?
easy
A. Define the method outside the module
B. Define the method without self inside the module
C. Define the method with self.method_name inside the module
D. Define the method with def module.method_name inside the module

Solution

  1. Step 1: Understand module method definition

    In Ruby, to make a method callable directly on a module, you define it with self.method_name inside the module.
  2. Step 2: Compare options

    Define the method with self.method_name inside the module uses self.method_name, which is correct. Other options either omit self or use invalid syntax.
  3. Final Answer:

    Define the method with self.method_name inside the module -> Option C
  4. Quick Check:

    Module methods use self.method_name = A [OK]
Hint: Use self.method_name to define module methods [OK]
Common Mistakes:
  • Forgetting to add self before method name
  • Defining methods outside the module
  • Using invalid syntax like def module.method_name
2. Which of the following is the correct syntax to call a module method named greet defined in module Welcome?
easy
A. Welcome#greet
B. greet.Welcome
C. Welcome.new.greet
D. Welcome.greet

Solution

  1. Step 1: Recall how to call module methods

    Module methods are called using dot notation: ModuleName.method_name.
  2. Step 2: Check options

    Welcome.greet is the correct syntax using dot notation. Welcome#greet incorrectly uses #. greet.Welcome reverses the order. Welcome.new.greet confuses it with an instance method call.
  3. Final Answer:

    Welcome.greet -> Option D
  4. Quick Check:

    Call module methods with dot = D [OK]
Hint: Call module methods with ModuleName.method_name [OK]
Common Mistakes:
  • Using # instead of . to call module methods
  • Trying to call method with invalid syntax
  • Confusing instance and module method calls
3. What will be the output of the following Ruby code?
module MathTools
  def self.square(x)
    x * x
  end
end

puts MathTools.square(4)
medium
A. 16
B. square
C. Error: undefined method
D. 4

Solution

  1. Step 1: Understand the method definition

    The method square is defined as a module method using self.square. It returns the square of the input.
  2. Step 2: Calculate the output

    Calling MathTools.square(4) returns 4 * 4 = 16, which is printed.
  3. Final Answer:

    16 -> Option A
  4. Quick Check:

    4 squared = 16 [OK]
Hint: Multiply input by itself in module method [OK]
Common Mistakes:
  • Forgetting self and causing undefined method error
  • Confusing output with input
  • Expecting method name as output
4. Identify the error in this Ruby module method code:
module Greetings
  def greet
    "Hello!"
  end
end

puts Greetings.greet
medium
A. Method greet is not defined as a module method
B. Module name is incorrect
C. Missing parentheses in method call
D. Syntax error in method body

Solution

  1. Step 1: Check method definition inside module

    The method greet is defined without self., so it is an instance method, not a module method.
  2. Step 2: Understand method call

    Calling Greetings.greet tries to call a module method, but none exists, causing an error.
  3. Final Answer:

    Method greet is not defined as a module method -> Option A
  4. Quick Check:

    Missing self. means no module method [OK]
Hint: Add self. to define module methods [OK]
Common Mistakes:
  • Defining instance methods instead of module methods
  • Calling module method without defining it
  • Assuming parentheses are required in method call
5. Given the module below, how can you add a module method double that returns twice the input number?
module Calculator
  # Add your method here
end
hard
A. def double(x) x + x end
B. def self.double(x) x * 2 end
C. def Calculator.double(x) x * 2 end
D. def self.double x * 2 end

Solution

  1. Step 1: Define a module method with self

    To create a module method, define it with self.method_name inside the module.
  2. Step 2: Write method to return twice the input

    The method should take one argument x and return x * 2.
  3. Final Answer:

    def self.double(x) x * 2 end -> Option B
  4. Quick Check:

    Module method with self and correct logic = A [OK]
Hint: Use def self.method(args) for module methods [OK]
Common Mistakes:
  • Omitting self and creating instance method
  • Missing method argument
  • Defining method outside module
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard