0
0
Rubyprogramming~20 mins

Inherited hook in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inherited Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A
Inherited called: 1
Inherited called: 2
B
Inherited called: 1
Inherited called: 1
C
Inherited called: 2
Inherited called: 3
DNo output
Attempts:
2 left
💡 Hint
Remember that class variables are shared among the class hierarchy.
Predict Output
intermediate
2: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
A
Subclasses: 0
Subclasses: 1
BNo output
C
Subclasses: 1
Subclasses: 2
D
Subclasses: 1
Subclasses: 1
Attempts:
2 left
💡 Hint
Class instance variables are stored on the class object and persist across multiple method calls.
🔧 Debug
advanced
2: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
ABecause @count is a class instance variable and is nil initially, so @count += 1 raises an error.
BBecause @count is not initialized before incrementing, so it is nil and nil + 1 causes an error.
CBecause @count is a class instance variable of Parent and is not shared with subclasses, but inherited is called on subclasses, so @count is nil each time.
DBecause inherited hook is not called for subclasses, so @count never increments.
Attempts:
2 left
💡 Hint
Check the initial value of @count before incrementing.
Predict Output
advanced
2: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
A
RuntimeError: wrong number of arguments (given 0, expected 1)
B
NoMethodError: undefined method `greet' for Child:Class
C
Hello from Parent
D
Hello from Child
Attempts:
2 left
💡 Hint
The inherited hook adds a class method to the subclass.
🧠 Conceptual
expert
2: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
A
Inherited: B
Inherited: C
B
Inherited: C
C
Inherited: B
DNo output
Attempts:
2 left
💡 Hint
The inherited hook is called for every direct subclass.