What if you could fix a bug once and see it fixed everywhere instantly?
Why Include for instance methods in Ruby? - Purpose & Use Cases
Imagine you have several classes in your Ruby program, and each class needs the same set of methods to work properly. You try to copy and paste these methods into every class manually.
This manual copying is slow and boring. If you want to change a method, you have to find and update it in every class separately. This causes mistakes and wastes time.
Using include lets you write the methods once in a module and then add them to any class easily. This way, all classes share the same methods, and updating them happens in one place.
class Dog def speak puts 'Hello!' end end class Cat def speak puts 'Hello!' end end
module Speakable def speak puts 'Hello!' end end class Dog include Speakable end class Cat include Speakable end
This makes your code cleaner, easier to maintain, and lets you add shared behavior to many classes quickly.
Think of a game where many characters can jump. Instead of writing jump code in every character class, you put it in a module and include it wherever needed.
Copying methods manually is slow and error-prone.
include shares methods across classes easily.
Updating shared methods happens in one place, saving time.
Practice
include keyword do in Ruby when used inside a class?Solution
Step 1: Understand the role of
Theincludeincludekeyword is used to add methods from a module into a class.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.Final Answer:
It adds the module's methods as instance methods to the class. -> Option DQuick Check:
Include = instance methods added [OK]
- Confusing include with extend (which adds class methods)
- Thinking include creates a new module
- Assuming include removes existing methods
Greetings inside a class Person?Solution
Step 1: Recall correct syntax for including modules
In Ruby, to add instance methods from a module, useinclude ModuleNameinside the class body without parentheses.Step 2: Check each option
class Person; include Greetings; end usesinclude Greetingscorrectly. class Person; extend Greetings; end usesextendwhich adds class methods, not instance methods. class Person; include(Greetings); end uses parentheses which is not standard syntax. class Person; use Greetings; end usesusewhich is invalid in Ruby.Final Answer:
class Person; include Greetings; end -> Option AQuick Check:
Include syntax = include ModuleName [OK]
- Using extend instead of include
- Adding parentheses after include
- Using invalid keywords like 'use'
module Talk
def speak
"Hello!"
end
end
class Dog
include Talk
end
puts Dog.new.speakSolution
Step 1: Understand module inclusion
The moduleTalkdefines an instance methodspeak. Including it inDogaddsspeakas an instance method ofDog.Step 2: Trace the method call
Creating a newDogobject and callingspeakwill execute the method fromTalk, returning "Hello!".Final Answer:
Hello! -> Option BQuick Check:
Included method called = "Hello!" [OK]
- Expecting NoMethodError because of missing method
- Confusing instance and class methods
- Thinking output is method name or nil
module Walk
def walk
"Walking"
end
end
class Person
extend Walk
end
puts Person.new.walkSolution
Step 1: Understand extend vs include
Usingextend Walkadds module methods as class methods, not instance methods.Step 2: Analyze method call
Person.new.walkcallswalkon an instance, butwalkis only a class method here, so it raises NoMethodError.Final Answer:
NoMethodError because walk is not an instance method -> Option AQuick Check:
Extend adds class methods, instance call fails [OK]
- Confusing extend with include
- Expecting no error on instance method call
- Thinking extend causes syntax or type errors
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
endSolution
Step 1: Check method definitions in classes
ClassDogoverridessoundmethod with "Bark". ClassCatdoes not override it.Step 2: Determine method lookup for Cat instance
SinceCatincludesAnimalSoundsand does not overridesound, callingsoundonCat.newuses the module's method returning "Generic sound".Final Answer:
Generic sound -> Option CQuick Check:
Included method used if no override [OK]
- Assuming Cat uses Dog's sound method
- Expecting NoMethodError for missing method
- Confusing instance and class methods
Ready to prove it?
Pass the test and this pattern moves unknown
