0
0
Rubyprogramming~10 mins

Module methods in Ruby - Step-by-Step Execution

Choose your learning style9 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.