Bird
Raised Fist0
Rubyprogramming~10 mins

Custom modules as mixins in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to include the module as a mixin in the class.

Ruby
module Greetings
  def greet
    "Hello!"
  end
end

class Person
  [1] Greetings
end
Drag options to blanks, or click blank then click option'
Aimport
Bextend
Crequire
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' adds methods as class methods, not instance methods.
Using 'require' or 'import' does not mix in methods.
2fill in blank
medium

Complete the code to call the mixin method from an instance.

Ruby
module Logger
  def log(message)
    "Log: #{message}"
  end
end

class App
  include Logger
end

app = App.new
puts app.[1]("Started")
Drag options to blanks, or click blank then click option'
Aprint
Blog
Cputs
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call 'print' or 'puts' as the method from the module.
Using 'message' which is a parameter, not a method.
3fill in blank
hard

Fix the error by choosing the correct keyword to add module methods as class methods.

Ruby
module Tools
  def info
    "Tool info"
  end

  def details
    "Details"
  end
end

class Machine
  [1] Tools
end

puts Machine.info
Drag options to blanks, or click blank then click option'
Aextend
Binclude
Crequire
Dimport
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' adds instance methods, so 'Machine.info' would fail.
Using 'require' or 'import' does not add methods to the class.
4fill in blank
hard

Fill both blanks to create a mixin module and include it in a class.

Ruby
module [1]
  def shout
    "Hey!"
  end
end

class [2]
  include Shoutable
end
Drag options to blanks, or click blank then click option'
AShoutable
BTalker
CSpeaker
DListener
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between module name and the name used in include.
Using unrelated class names.
5fill in blank
hard

Fill all three blanks to define a module with an instance method, include it in a class, and call the method.

Ruby
module [1]
  def greet
    "Hi!"
  end
end

class [2]
  [3] Greetings
end

obj = Greeter.new
puts obj.greet
Drag options to blanks, or click blank then click option'
AGreetings
BGreeter
Cinclude
Dextend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' for instance methods.
Mismatch in module or class names.

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

  1. Step 1: Understand what a module is

    A module groups methods that can be reused in different classes.
  2. Step 2: Recognize the role of mixins

    Mixins let classes include these modules to share behavior without inheritance.
  3. Final Answer:

    To share methods across multiple classes without repeating code -> Option A
  4. 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

  1. Step 1: Identify how to add instance methods from a module

    Use include inside the class to add module methods as instance methods.
  2. Step 2: Check syntax correctness

    Only class Person; include Greetings; end is valid Ruby syntax for this purpose.
  3. Final Answer:

    class Person; include Greetings; end -> Option D
  4. 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

  1. Step 1: Understand module inclusion

    The Talk module defines speak method. Including it in Dog adds speak as an instance method.
  2. Step 2: Call the method on Dog instance

    Dog.new.speak calls the method and returns "Hello!" string.
  3. Final Answer:

    Hello! -> Option B
  4. 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

  1. Step 1: Understand extend vs include

    extend Helper adds methods as class methods, not instance methods.
  2. Step 2: Analyze method call on instance

    User.new.help calls help on an instance, but method is only on class, causing NoMethodError.
  3. Final Answer:

    Using extend instead of include causes NoMethodError on instance -> Option A
  4. 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

  1. Step 1: Understand including multiple modules

    You can include multiple modules by calling include multiple times inside the class.
  2. Step 2: Verify method calls on instance

    Including modules adds their methods as instance methods, so p.walk and p.speak work.
  3. Final Answer:

    class Person include Walk include Talk end p = Person.new puts p.walk puts p.speak -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard