0
0
Rubyprogramming~10 mins

Extend for class methods in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extend for class methods
Define Module with methods
Create Class
Extend Class with Module
Call Module methods on Class
Methods run as class methods
This flow shows how a module's methods become class methods when a class extends that module.
Execution Sample
Ruby
module Greetings
  def hello
    "Hello from class!"
  end
end

class Person
  extend Greetings
end

puts Person.hello
This code adds the module's method as a class method to Person and calls it.
Execution Table
StepActionEvaluationResult
1Define module Greetings with method helloModule createdModule Greetings with method hello defined
2Define class PersonClass createdClass Person defined
3Extend Person with GreetingsPerson gains module methods as class methodsPerson.hello method available
4Call Person.helloMethod runs"Hello from class!" printed
5End of programNo more codeProgram ends
💡 Program ends after printing the class method result
Variable Tracker
VariableStartAfter extendAfter method callFinal
PersonClass definedHas class method helloMethod calledClass unchanged
Key Moments - 2 Insights
Why does Person.hello work after extend but not before?
Because extend adds module methods as class methods to Person, shown in step 3 of execution_table.
Is the hello method available to instances of Person?
No, extend adds methods only to the class itself, not its instances.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
APerson gains instance methods from Greetings
BPerson loses all methods
CPerson gains class methods from Greetings
DGreetings module is deleted
💡 Hint
See step 3 in execution_table where extend adds module methods as class methods
At which step is the method hello actually called?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Step 4 shows calling Person.hello and printing the result
If we used include instead of extend, what would change?
Ahello becomes a class method
Bhello becomes an instance method
Chello is not added at all
DProgram crashes
💡 Hint
Extend adds class methods; include adds instance methods
Concept Snapshot
Use extend to add module methods as class methods.
Syntax: class MyClass; extend MyModule; end
Methods in MyModule become callable on MyClass itself.
Unlike include, which adds instance methods.
Useful to share behavior at class level.
Full Transcript
This example shows how to use extend in Ruby to add methods from a module as class methods to a class. First, a module Greetings is defined with a method hello. Then, a class Person is defined. Using extend Greetings inside Person adds the hello method as a class method. When Person.hello is called, it runs the module method and prints the message. The execution table traces each step: defining the module, defining the class, extending the class, calling the method, and ending the program. The variable tracker shows Person gains the class method after extend. Key moments clarify that extend adds class methods, not instance methods. The quiz tests understanding of when methods are added and called, and the difference between extend and include. The snapshot summarizes the key points for quick reference.