Bird
Raised Fist0
Rubyprogramming~10 mins

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

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

Practice

(1/5)
1. What does the include keyword do in Ruby when used inside a class?
easy
A. It deletes all methods from the class.
B. It adds the module's methods as class methods to the class.
C. It creates a new module inside the class.
D. It adds the module's methods as instance methods to the class.

Solution

  1. Step 1: Understand the role of include

    The include keyword is used to add methods from a module into a class.
  2. Step 2: Identify how methods are added

    When a module is included, its methods become instance methods of the class, meaning objects of the class can call them.
  3. Final Answer:

    It adds the module's methods as instance methods to the class. -> Option D
  4. Quick Check:

    Include = instance methods added [OK]
Hint: Include adds module methods as instance methods [OK]
Common Mistakes:
  • Confusing include with extend (which adds class methods)
  • Thinking include creates a new module
  • Assuming include removes existing methods
2. Which of the following is the correct syntax to include a module named Greetings inside a class Person?
easy
A. class Person; include Greetings; end
B. class Person; extend Greetings; end
C. class Person; include(Greetings); end
D. class Person; use Greetings; end

Solution

  1. Step 1: Recall correct syntax for including modules

    In Ruby, to add instance methods from a module, use include ModuleName inside the class body without parentheses.
  2. Step 2: Check each option

    class Person; include Greetings; end uses include Greetings correctly. class Person; extend Greetings; end uses extend which adds class methods, not instance methods. class Person; include(Greetings); end uses parentheses which is not standard syntax. class Person; use Greetings; end uses use which is invalid in Ruby.
  3. Final Answer:

    class Person; include Greetings; end -> Option A
  4. Quick Check:

    Include syntax = include ModuleName [OK]
Hint: Use 'include ModuleName' without parentheses [OK]
Common Mistakes:
  • Using extend instead of include
  • Adding parentheses after include
  • Using invalid keywords like 'use'
3. What will be the output of the following Ruby code?
module Talk
  def speak
    "Hello!"
  end
end

class Dog
  include Talk
end

puts Dog.new.speak
medium
A. NoMethodError
B. Hello!
C. speak
D. nil

Solution

  1. Step 1: Understand module inclusion

    The module Talk defines an instance method speak. Including it in Dog adds speak as an instance method of Dog.
  2. Step 2: Trace the method call

    Creating a new Dog object and calling speak will execute the method from Talk, returning "Hello!".
  3. Final Answer:

    Hello! -> Option B
  4. Quick Check:

    Included method called = "Hello!" [OK]
Hint: Included module methods become instance methods [OK]
Common Mistakes:
  • Expecting NoMethodError because of missing method
  • Confusing instance and class methods
  • Thinking output is method name or nil
4. Identify the error in the following Ruby code:
module Walk
  def walk
    "Walking"
  end
end

class Person
  extend Walk
end

puts Person.new.walk
medium
A. NoMethodError because walk is not an instance method
B. SyntaxError due to extend usage
C. No error, outputs "Walking"
D. TypeError because module cannot be extended

Solution

  1. Step 1: Understand extend vs include

    Using extend Walk adds module methods as class methods, not instance methods.
  2. Step 2: Analyze method call

    Person.new.walk calls walk on an instance, but walk is only a class method here, so it raises NoMethodError.
  3. Final Answer:

    NoMethodError because walk is not an instance method -> Option A
  4. Quick Check:

    Extend adds class methods, instance call fails [OK]
Hint: Extend adds class methods; instance calls fail [OK]
Common Mistakes:
  • Confusing extend with include
  • Expecting no error on instance method call
  • Thinking extend causes syntax or type errors
5. Given the module and classes below, what will puts Cat.new.sound output?
module AnimalSounds
  def sound
    "Generic sound"
  end
end

class Dog
  include AnimalSounds
  def sound
    "Bark"
  end
end

class Cat
  include AnimalSounds
end
hard
A. Bark
B. NoMethodError
C. Generic sound
D. nil

Solution

  1. Step 1: Check method definitions in classes

    Class Dog overrides sound method with "Bark". Class Cat does not override it.
  2. Step 2: Determine method lookup for Cat instance

    Since Cat includes AnimalSounds and does not override sound, calling sound on Cat.new uses the module's method returning "Generic sound".
  3. Final Answer:

    Generic sound -> Option C
  4. Quick Check:

    Included method used if no override [OK]
Hint: Class method overrides module method if defined [OK]
Common Mistakes:
  • Assuming Cat uses Dog's sound method
  • Expecting NoMethodError for missing method
  • Confusing instance and class methods
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