A mixin is a module that you include in a class to add reusable methods without using inheritance.
Click to reveal answer
beginner
How do you include a custom module as a mixin in a Ruby class?
Use the <code>include</code> keyword inside the class to add the module's methods as instance methods.
Click to reveal answer
intermediate
What is the difference between include and extend when using modules?
<code>include</code> adds module methods as instance methods; <code>extend</code> adds them as class methods.
Click to reveal answer
beginner
Can a Ruby class include multiple modules as mixins?
Yes, a class can include many modules to combine different sets of methods.
Click to reveal answer
intermediate
Why use custom modules as mixins instead of inheritance?
Mixins let you share behavior across unrelated classes without forcing a strict parent-child relationship.
Click to reveal answer
How do you add a module's methods as instance methods in a Ruby class?
AUsing <code>import</code>
BUsing <code>extend</code>
CUsing <code>inherit</code>
DUsing <code>include</code>
✗ Incorrect
Use include to add module methods as instance methods.
What keyword adds module methods as class methods to a Ruby class?
Aextend
Bmix
Cinclude
Duse
✗ Incorrect
extend adds module methods as class methods.
Which of these is true about Ruby modules used as mixins?
AThey allow multiple inheritance.
BThey only work with subclasses.
CThey let you share methods across classes.
DThey replace classes.
✗ Incorrect
Modules let you share methods across different classes without inheritance.
Can a Ruby class include more than one module?
ANo, only one module is allowed.
BYes, multiple modules can be included.
COnly if they are related modules.
DOnly if they are in the same file.
✗ Incorrect
Ruby classes can include many modules to combine behaviors.
Why might you use a module as a mixin instead of inheritance?
ATo share behavior without a parent-child relationship.
BTo avoid code reuse.
CTo make classes unrelated.
DTo prevent method overriding.
✗ Incorrect
Mixins share behavior across classes without forcing inheritance.
Explain how to create and use a custom module as a mixin in Ruby.
Think about how you add shared behavior to different classes.
You got /3 concepts.
Describe the difference between include and extend when using modules in Ruby.
Consider who can call the methods after adding the module.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a custom module as a mixin in Ruby?
easy
A. To share methods across multiple classes without repeating code
B. To create a new class that inherits from multiple classes
C. To define global variables accessible everywhere
D. To replace the need for classes entirely
Solution
Step 1: Understand what a module is
A module groups methods that can be reused in different classes.
Step 2: Recognize the role of mixins
Mixins let classes include these modules to share behavior without inheritance.
Final Answer:
To share methods across multiple classes without repeating code -> Option A
Quick Check:
Modules as mixins = code sharing [OK]
Hint: Modules share methods to avoid repeating code [OK]
Common Mistakes:
Confusing modules with multiple inheritance
Thinking modules define global variables
Believing modules replace classes
2. Which of the following is the correct syntax to include a module named Greetings inside a class Person?
easy
A. module Person; include Greetings; end
B. class Person; extend Greetings; end
C. class Person; use Greetings; end
D. class Person; include Greetings; end
Solution
Step 1: Identify how to add instance methods from a module
Use include inside the class to add module methods as instance methods.
Step 2: Check syntax correctness
Only class Person; include Greetings; end is valid Ruby syntax for this purpose.
Final Answer:
class Person; include Greetings; end -> Option D
Quick Check:
Include module inside class = class Person; include Greetings; end [OK]
Hint: Use include to add module methods as instance methods [OK]
Common Mistakes:
Using extend instead of include for instance methods
Writing module instead of class
Using non-existent keyword '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
Step 1: Understand module inclusion
The Talk module defines speak method. Including it in Dog adds speak as an instance method.
Step 2: Call the method on Dog instance
Dog.new.speak calls the method and returns "Hello!" string.
Final Answer:
Hello! -> Option B
Quick Check:
Included method call = "Hello!" [OK]
Hint: Included module methods become instance methods [OK]
Common Mistakes:
Expecting NoMethodError because of missing method
Confusing include with extend
Thinking output is method name or nil
4. Identify the error in the following Ruby code that tries to use a module as a mixin:
module Helper
def help
"Helping"
end
end
class User
extend Helper
end
puts User.new.help
medium
A. Using extend instead of include causes NoMethodError on instance
B. Module Helper is not defined properly
C. User class should be a module
D. help method should be a class method
Solution
Step 1: Understand extend vs include
extend Helper adds methods as class methods, not instance methods.
Step 2: Analyze method call on instance
User.new.help calls help on an instance, but method is only on class, causing NoMethodError.
Final Answer:
Using extend instead of include causes NoMethodError on instance -> Option A
Quick Check:
extend adds class methods, not instance methods [OK]
Hint: Use include for instance methods, extend for class methods [OK]
Common Mistakes:
Confusing extend and include
Calling instance method on class method only
Thinking module definition is wrong
5. Given two modules Walk and Talk with methods walk and speak respectively, how can you create a class Person that uses both modules as mixins and calls both methods on an instance?
Choose the correct code snippet.
hard
A. class Person
extend Walk
extend Talk
end
p = Person.new
puts p.walk
puts p.speak
B. class Person
include Walk Talk
end
p = Person.new
puts p.walk
puts p.speak
C. class Person
include Walk
include Talk
end
p = Person.new
puts p.walk
puts p.speak
D. class Person
use Walk
use Talk
end
p = Person.new
puts p.walk
puts p.speak
Solution
Step 1: Understand including multiple modules
You can include multiple modules by calling include multiple times inside the class.
Step 2: Verify method calls on instance
Including modules adds their methods as instance methods, so p.walk and p.speak work.
Final Answer:
class Person
include Walk
include Talk
end
p = Person.new
puts p.walk
puts p.speak -> Option C
Quick Check:
Include modules separately for instance methods [OK]
Hint: Include each module separately for instance methods [OK]
Common Mistakes:
Using extend instead of include for instance methods
Trying to include multiple modules in one line
Using non-existent keyword 'use'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.