Challenge - 5 Problems
Inherited Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inherited hook with class variable
What is the output of this Ruby code using the inherited hook and a class variable?
Ruby
class Parent @@count = 0 def self.inherited(subclass) @@count += 1 puts "Inherited called: #{@@count}" end end class Child1 < Parent; end class Child2 < Parent; end
Attempts:
2 left
💡 Hint
Remember that class variables are shared among the class hierarchy.
✗ Incorrect
The inherited hook is called each time a subclass is created. The class variable @@count is shared, so it increments with each subclass.
❓ Predict Output
intermediate2:00remaining
Value of class instance variable in inherited hook
What will be printed when the following Ruby code runs?
Ruby
class Base @count = 0 def self.inherited(subclass) @count ||= 0 @count += 1 puts "Subclasses: #{@count}" end end class Sub1 < Base; end class Sub2 < Base; end
Attempts:
2 left
💡 Hint
Class instance variables are stored on the class object and persist across multiple method calls.
✗ Incorrect
The @count variable belongs to the Base class object and persists between calls to inherited, which is invoked on Base for each direct subclass. It increments from 1 to 2.
🔧 Debug
advanced2:00remaining
Why does this inherited hook not count subclasses correctly?
This code is intended to count how many subclasses inherit from Parent, but it raises a NoMethodError. Why?
Ruby
class Parent def self.inherited(subclass) @count += 1 puts "Count: #{@count}" end end class ChildA < Parent; end class ChildB < Parent; end
Attempts:
2 left
💡 Hint
Check the initial value of @count before incrementing.
✗ Incorrect
The @count variable is nil initially. Trying to add 1 to nil causes a NoMethodError. It must be initialized before incrementing.
❓ Predict Output
advanced2:00remaining
Effect of inherited hook on subclass method
What is the output of this Ruby code that uses the inherited hook to add a method to subclasses?
Ruby
class Parent def self.inherited(subclass) subclass.define_singleton_method(:greet) do "Hello from #{subclass}" end end end class Child < Parent; end puts Child.greet
Attempts:
2 left
💡 Hint
The inherited hook adds a class method to the subclass.
✗ Incorrect
The inherited hook defines a singleton method greet on the subclass, so calling Child.greet returns the expected string.
🧠 Conceptual
expert2:00remaining
Understanding inherited hook behavior with multiple levels
Consider this Ruby code with multiple inheritance levels. How many times is the inherited hook called and what is printed?
Ruby
class A def self.inherited(subclass) puts "Inherited: #{subclass}" end end class B < A; end class C < B; end
Attempts:
2 left
💡 Hint
The inherited hook is called for every direct subclass.
✗ Incorrect
The inherited hook in A is called when B inherits from A, and again when C inherits from B (because B inherits from A, so B's inherited hook is inherited from A).