0
0
Rubyprogramming~10 mins

Include for instance methods in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Include for instance methods
Define Module with Instance Methods
Define Class
Include Module in Class
Create Object of Class
Call Included Instance Method on Object
Method Executes from Module
This flow shows how a module with instance methods is included in a class, then called on an object of that class.
Execution Sample
Ruby
module Greetings
  def hello
    "Hello from module!"
  end
end

class Person
  include Greetings
end

p = Person.new
puts p.hello
This code defines a module with an instance method, includes it in a class, creates an object, and calls the method.
Execution Table
StepActionEvaluationResult
1Define module Greetings with method helloModule createdModule Greetings with instance method hello defined
2Define class PersonClass createdClass Person defined
3Include Greetings module in PersonModule includedPerson now has instance method hello from Greetings
4Create object p = Person.newObject createdp is a Person object
5Call p.helloMethod lookup finds hello in Greetings"Hello from module!" printed
6End of programNo more codeExecution stops
💡 Program ends after printing the string from the included instance method
Variable Tracker
VariableStartAfter Step 4After Step 5Final
pundefinedPerson object createdPerson object unchangedPerson object unchanged
Key Moments - 2 Insights
Why can we call p.hello even though hello is not defined in Person class?
Because the module Greetings was included in Person (see execution_table step 3), its instance methods become available to Person objects.
Is the method hello a class method or instance method of Person?
It is an instance method, because modules included with include add instance methods to the class (see execution_table step 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what does p.hello return?
AError: method not found
B"Hello from module!"
C"Hello from Person!"
Dnil
💡 Hint
Check the Result column at step 5 in execution_table
At which step is the module included into the class?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the Action column in execution_table for the inclusion action
If we remove 'include Greetings' from Person, what happens when calling p.hello?
AIt prints "Hello from module!"
BIt prints nil
CIt raises a NoMethodError
DIt calls a different method
💡 Hint
Without inclusion, the method hello is not found on Person instances (see execution_table step 3 and 5)
Concept Snapshot
module ModuleName
  def instance_method
    # code
  end
end

class ClassName
  include ModuleName  # adds instance methods
end

obj = ClassName.new
obj.instance_method  # calls method from module
Full Transcript
This example shows how to use 'include' in Ruby to add instance methods from a module to a class. First, a module named Greetings is defined with an instance method hello. Then, a class Person is defined and includes the Greetings module. This means Person objects can call hello even though it is not defined directly in Person. When we create a Person object p and call p.hello, Ruby finds the method in the included module and runs it, printing "Hello from module!". The execution table traces each step: defining the module, defining the class, including the module, creating the object, and calling the method. The variable tracker shows the object p being created and used. Key moments clarify why the method is available and that it is an instance method. The quiz questions check understanding of method lookup, inclusion step, and what happens if inclusion is removed.