0
0
Rubyprogramming~10 mins

Included hook in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Included hook
Define Module with included method
Include Module in Class
Ruby calls included hook
Execute included method code
Modify Class (e.g., add methods)
Class ready with module features
When a module is included in a class, Ruby automatically calls the module's included method, allowing the module to modify the class.
Execution Sample
Ruby
module M
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def greet
      "Hello from class"
    end
  end
end

class C
  include M
end

puts C.greet
This code shows a module with an included hook that adds class methods to the including class.
Execution Table
StepActionEvaluationResult
1Define module M with included methodModule M created with included hookModule M ready
2Define ClassMethods module inside MClassMethods module createdClassMethods ready
3Define class CClass C createdClass C ready
4Include module M in class CRuby calls M.included(C)C extended with ClassMethods
5Call C.greetC.greet method found in ClassMethods"Hello from class" printed
6Program endsNo more codeExecution stops
💡 Program ends after printing the greeting from C.greet
Variable Tracker
VariableStartAfter Step 4After Step 5Final
MModule definedIncluded hook readyNo changeNo change
CClass definedExtended with ClassMethodsMethod greet availableNo change
Key Moments - 2 Insights
Why does the included method get called automatically?
Because Ruby calls the included method on the module whenever it is included in a class, as shown in step 4 of the execution_table.
How does the class get new methods from the module?
The included method extends the class with ClassMethods module, adding methods like greet, as seen in step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 4?
AThe module M is defined
BThe included hook is called and class C is extended
CThe greet method is called
DThe program ends
💡 Hint
Check the Action and Result columns at step 4 in the execution_table
According to variable_tracker, what changes happen to class C after step 4?
AC is extended with ClassMethods module
BC is deleted
CC loses all methods
DNo change to C
💡 Hint
Look at the 'After Step 4' column for variable C in variable_tracker
If the included method was removed from module M, what would happen when including M in C?
AC would still get greet method
BProgram would crash
CC would not get greet method
Dincluded method would be called anyway
💡 Hint
Refer to how included hook adds ClassMethods in execution_table step 4
Concept Snapshot
module M
  def self.included(base)
    base.extend(ClassMethods) # runs when included
  end

  module ClassMethods
    def greet
      "Hello from class"
    end
  end
end

class C
  include M # triggers included hook
end

C.greet # => "Hello from class"
Full Transcript
This example shows how Ruby calls the included method automatically when a module is included in a class. The included method receives the class as an argument and can modify it, for example by extending it with additional methods. Here, module M defines an included method that extends the including class with ClassMethods, adding a greet method. When class C includes M, Ruby calls M.included(C), which adds greet to C. Calling C.greet then prints the greeting. This hook is useful to add class methods or perform setup when a module is included.