0
0
Rubyprogramming~20 mins

Included hook in Ruby - Practice Problems & Coding Challenges

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