Bird
Raised Fist0
Rubyprogramming~20 mins

Inherited hook in Ruby - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the inherited hook method do in Ruby?
easy
A. It is called automatically when a subclass is created.
B. It runs when an instance of a class is created.
C. It is used to define class variables.
D. It is called when a method is overridden.

Solution

  1. Step 1: Understand the purpose of inherited

    The inherited method is a special hook in Ruby that triggers when a subclass is created from a parent class.
  2. Step 2: Differentiate from other hooks

    It is not related to instance creation, method overriding, or variable definition, but specifically to subclass creation.
  3. Final Answer:

    It is called automatically when a subclass is created. -> Option A
  4. Quick Check:

    Inherited hook = called on subclass creation [OK]
Hint: Inherited hook triggers only on subclass creation [OK]
Common Mistakes:
  • Confusing inherited with initialize method
  • Thinking it runs on instance creation
  • Mixing it with method override hooks
2. Which of the following is the correct way to define an inherited hook in a Ruby class?
easy
A. def self.inherited; puts "New subclass created"; end
B. def inherited(self); puts "New subclass created"; end
C. def inherited(subclass); puts "New subclass created"; end
D. def self.inherited(subclass); puts "New subclass created"; end

Solution

  1. Step 1: Recognize the method signature

    The inherited hook must be defined as a class method, so it needs self.inherited and takes one argument: the subclass.
  2. Step 2: Check method parameters and syntax

    def self.inherited(subclass); puts "New subclass created"; end correctly defines the class method with the subclass parameter. Incorrect versions lack self. (instance method), omit the subclass parameter, or use wrong parameters like self.
  3. Final Answer:

    def self.inherited(subclass); puts "New subclass created"; end -> Option D
  4. Quick Check:

    Class method with subclass param = def self.inherited(subclass); puts "New subclass created"; end [OK]
Hint: Inherited hook is a class method with subclass param [OK]
Common Mistakes:
  • Defining inherited as instance method
  • Omitting the subclass parameter
  • Forgetting self. for class method
3. What will be the output of this Ruby code?
class Parent
  def self.inherited(subclass)
    puts "Subclass: #{subclass}"
  end
end

class Child < Parent
end
medium
A. No output
B. Subclass: Child
C. Subclass: Parent
D. SyntaxError

Solution

  1. Step 1: Understand when inherited runs

    The inherited method in Parent runs automatically when a subclass is created.
  2. Step 2: Identify subclass creation

    When Child < Parent is declared, Parent.inherited(Child) is called, printing "Subclass: Child".
  3. Final Answer:

    Subclass: Child -> Option B
  4. Quick Check:

    Subclass creation triggers inherited hook output [OK]
Hint: Inherited prints subclass name on subclass creation [OK]
Common Mistakes:
  • Expecting output on instance creation
  • Confusing subclass with parent class name
  • Thinking no output occurs
4. Identify the error in this Ruby code using the inherited hook:
class Base
  def inherited(subclass)
    puts "Subclass created: #{subclass}"
  end
end

class Derived < Base
end
medium
A. The inherited method should be a class method with self.
B. The subclass parameter is missing.
C. The puts statement syntax is incorrect.
D. Derived class should not inherit from Base.

Solution

  1. Step 1: Check method definition context

    The inherited hook must be a class method, so it requires def self.inherited(subclass).
  2. Step 2: Identify the error in the code

    Here, inherited is defined as an instance method, missing self., so it won't be called on subclass creation.
  3. Final Answer:

    The inherited method should be a class method with self. -> Option A
  4. Quick Check:

    Inherited hook must be class method [OK]
Hint: Inherited must be defined with self. to work [OK]
Common Mistakes:
  • Defining inherited as instance method
  • Omitting self. in method definition
  • Misunderstanding inherited hook usage
5. How can you use the inherited hook to automatically add a class variable @@subclasses that tracks all subclasses of a parent class?
hard
A. Use initialize method to track subclasses in @@subclasses.
B. Define an instance method inherited to add subclass to @@subclasses.
C. Define self.inherited(subclass) to append subclass to @@subclasses array.
D. Manually add subclasses to @@subclasses outside the class.

Solution

  1. Step 1: Understand subclass tracking

    To track subclasses, use a class variable @@subclasses initialized as an array in the parent class.
  2. Step 2: Use inherited hook to update tracking

    Define self.inherited(subclass) to append the new subclass to @@subclasses automatically when a subclass is created.
  3. Final Answer:

    Define self.inherited(subclass) to append subclass to @@subclasses array. -> Option C
  4. Quick Check:

    Inherited hook + class variable tracks subclasses [OK]
Hint: Use inherited hook to update subclass list automatically [OK]
Common Mistakes:
  • Using instance methods to track subclasses
  • Tracking subclasses in initialize method
  • Forgetting to initialize @@subclasses as array
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