Bird
Raised Fist0
Rubyprogramming~20 mins

Included hook in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Included Hook Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using the included hook?

Consider this Ruby module and class. What will be printed when MyClass.new.greet is called?

Ruby
module Greeter
  def self.included(base)
    puts "Greeter included in #{base}"
  end

  def greet
    "Hello!"
  end
end

class MyClass
  include Greeter
end

puts MyClass.new.greet
A
Greeter included in MyClass
Hello!
BHello!
C
Greeter included in Greeter
Hello!
DNo output, only returns "Hello!"
Attempts:
2 left
💡 Hint

Remember that self.included runs when the module is included in a class.

❓ Predict Output
intermediate
2:00remaining
What happens when including a module with an included hook that modifies the base class?

What will be the output of this Ruby code?

Ruby
module Tracker
  def self.included(base)
    base.class_eval do
      def tracked_method
        "Tracked method called"
      end
    end
  end
end

class Widget
  include Tracker
end

puts Widget.new.tracked_method
ASyntaxError due to class_eval usage
BNoMethodError: undefined method `tracked_method' for #<Widget:...>
CTracked method called
DRuntimeError: included hook failed
Attempts:
2 left
💡 Hint

The included hook can add methods to the including class dynamically.

❓ Predict Output
advanced
2:00remaining
What is the output when multiple modules with included hooks are included?

Given these modules and class, what is printed when Example.new.test is called?

Ruby
module A
  def self.included(base)
    puts "Module A included"
  end
  def test
    "A"
  end
end

module B
  def self.included(base)
    puts "Module B included"
  end
  def test
    "B"
  end
end

class Example
  include A
  include B
end

puts Example.new.test
A
Module A included
Module B included
B
B
Module B included
Module A included
A
C
Module A included
Module B included
A
D
Module B included
Module A included
B
Attempts:
2 left
💡 Hint

Modules included later override methods of earlier modules.

❓ Predict Output
advanced
2:00remaining
What error occurs if included hook tries to call an undefined method on base?

What error will this Ruby code raise?

Ruby
module M
  def self.included(base)
    base.undefined_method
  end
end

class C
  include M
end
ASyntaxError: unexpected method call
BRuntimeError: included hook failed
CNo error, code runs silently
DNoMethodError: undefined method `undefined_method' for C:Class
Attempts:
2 left
💡 Hint

Calling a method that does not exist on a class raises a NoMethodError.

🧠 Conceptual
expert
2:00remaining
How does Ruby's included hook differ from extend in modules?

Which statement best describes the difference between include with an included hook and extend in Ruby modules?

A<code>include</code> adds class methods and triggers <code>included</code> hook; <code>extend</code> adds instance methods without triggering <code>included</code>.
B<code>include</code> adds instance methods and triggers <code>included</code> hook; <code>extend</code> adds class methods without triggering <code>included</code>.
C<code>include</code> and <code>extend</code> both add instance methods but only <code>extend</code> triggers <code>included</code> hook.
D<code>include</code> adds instance methods without triggering <code>included</code>; <code>extend</code> adds class methods and triggers <code>included</code> hook.
Attempts:
2 left
💡 Hint

Think about what methods are added and when the included hook runs.

Practice

(1/5)
1.

What does the included hook do in a Ruby module?

easy
A. It runs only when the module is extended, not included.
B. It runs code automatically when the module is included in a class.
C. It prevents the module from being included in any class.
D. It defines a method that must be called manually.

Solution

  1. Step 1: Understand the purpose of the included hook

    The included hook is a special method in Ruby modules that runs automatically when the module is included in a class.
  2. Step 2: Compare options with the definition

    Only It runs code automatically when the module is included in a class. correctly describes this automatic behavior. Other options describe manual calls, prevention, or extension, which are incorrect.
  3. Final Answer:

    It runs code automatically when the module is included in a class. -> Option B
  4. Quick Check:

    Included hook = automatic run on include [OK]
Hint: Remember: included runs automatically on module inclusion [OK]
Common Mistakes:
  • Thinking included runs on extend instead of include
  • Believing included requires manual method calls
  • Confusing included with preventing inclusion
2.

Which of the following is the correct syntax to define an included hook inside a Ruby module?

module Example
  # What goes here?
end
easy
A. def self.included(base) puts "Included in #{base}" end
B. def included(base) puts "Included in #{base}" end
C. def included puts "Included" end
D. def self.include(base) puts "Included in #{base}" end

Solution

  1. Step 1: Recall how to define class methods in modules

    The included hook must be a module method, so it needs to be defined with self.included.
  2. Step 2: Check the method signature

    The method receives the including class as a parameter, so it must accept one argument (base).
  3. Final Answer:

    def self.included(base) puts "Included in #{base}" end -> Option A
  4. Quick Check:

    included hook = def self.included(base) [OK]
Hint: Use def self.included(base) for included hook syntax [OK]
Common Mistakes:
  • Defining included without self (instance method instead of module method)
  • Missing the base parameter
  • Using def self.include instead of included
3.

What will be the output of this Ruby code?

module Greetings
  def self.included(base)
    puts "Greetings included in #{base}"
  end
end

class Person
  include Greetings
end
medium
A. SyntaxError
B. No output
C. Greetings included in Person
D. Greetings included in Greetings

Solution

  1. Step 1: Identify when included hook runs

    The included hook runs automatically when the module is included in a class.
  2. Step 2: Check the output inside the included method

    The method prints "Greetings included in #{base}", where base is the class Person.
  3. Final Answer:

    Greetings included in Person -> Option C
  4. Quick Check:

    included hook prints class name on include [OK]
Hint: included hook prints message with class name on include [OK]
Common Mistakes:
  • Expecting no output because included is a hook
  • Confusing base with module name
  • Thinking included runs on class definition, not include
4.

Find the error in this Ruby code using the included hook:

module Logger
  def included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def log(msg)
      puts "Log: #{msg}"
    end
  end
end

class App
  include Logger
end

App.log("Start")
medium
A. The include statement should be extend.
B. The ClassMethods module should be included, not extended.
C. The log method should be an instance method, not class method.
D. The included method should be defined as self.included, not instance method.

Solution

  1. Step 1: Check how included hook is defined

    The included hook must be a module method, defined as self.included, not an instance method.
  2. Step 2: Understand the effect of incorrect definition

    Defining included without self means it won't run automatically on include, so base.extend won't happen.
  3. Final Answer:

    The included method should be defined as self.included, not instance method. -> Option D
  4. Quick Check:

    included hook must be self.included [OK]
Hint: Define included as self.included to run on module include [OK]
Common Mistakes:
  • Defining included without self
  • Confusing extend and include in hook
  • Trying to call class method on instance
5.

You want to add a class method greet to any class that includes your module Friendly. Which is the best way to do this using the included hook?

module Friendly
  # Fill in the included hook
end

class Dog
  include Friendly
end

puts Dog.greet
hard
A. Define self.included(base) and inside it call base.extend with a module containing greet.
B. Define an instance method greet inside Friendly module.
C. Define a class method greet directly inside Friendly module without included hook.
D. Call include Friendly inside Dog.greet method.

Solution

  1. Step 1: Understand how to add class methods via included hook

    To add class methods, define self.included(base) and call base.extend with a module containing those methods.
  2. Step 2: Why other options fail

    Instance methods won't be class methods; defining class methods directly in module won't add them to including class; calling include inside method is incorrect.
  3. Final Answer:

    Define self.included(base) and inside it call base.extend with a module containing greet. -> Option A
  4. Quick Check:

    Use included hook + base.extend for class methods [OK]
Hint: Use included hook with base.extend to add class methods [OK]
Common Mistakes:
  • Defining greet as instance method instead of class method
  • Not using included hook to extend class
  • Trying to call include inside a method
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