Using a guard variable to track if method_added is already running prevents infinite recursion.
What type of argument does method_added receive?
AMethod body as a string
BMethod name as a symbol
CClass name as a string
DNo arguments
✗ Incorrect
method_added receives the name of the method just added as a symbol.
Where should method_added be defined to work properly?
AInside the class or module as a class method
BOutside any class or module
CInside an instance method
DInside a block passed to <code>define_method</code>
✗ Incorrect
method_added must be defined as a class method inside the class or module.
Which of these is a common use of method_added?
ACreating new classes
BDeleting methods from a class
CAutomatically wrapping new methods with extra behavior
DChanging variable values
✗ Incorrect
method_added is often used to wrap or modify methods right after they are defined.
Explain how the method_added hook works and give an example of when you might use it.
Think about how Ruby tells you when a new method appears.
You got /4 concepts.
Describe a potential problem when defining methods inside method_added and how to avoid it.
What happens if the hook triggers itself repeatedly?
You got /4 concepts.
Practice
(1/5)
1. What does the method_added hook do in Ruby?
easy
A. It runs when a class is inherited by another class.
B. It runs automatically whenever a new instance method is defined in a class.
C. It runs when a method is called on an object.
D. It runs when a module is included in a class.
Solution
Step 1: Understand the purpose of method_added
The method_added hook is a special method in Ruby that triggers automatically when a new instance method is defined inside a class.
Step 2: Compare with other hooks
Other hooks like inherited or included run on inheritance or module inclusion, not on method definition.
Final Answer:
It runs automatically whenever a new instance method is defined in a class. -> Option B
Quick Check:
method_added triggers on method definition [OK]
Hint: Remember: method_added triggers on new instance methods [OK]
Common Mistakes:
Confusing method_added with inherited or included hooks
Thinking it runs when methods are called
Assuming it triggers on class creation
2. Which of the following is the correct way to define a method_added hook inside a Ruby class?
easy
A. def self.method_added(method_name); puts "Added: #{method_name}"; end
B. def self.method_added; puts "Added method"; end
C. def method_added(method_name); puts "Added: #{method_name}"; end
D. def method_added; puts "Added method"; end
Solution
Step 1: Identify the correct method signature
The method_added hook is an instance method of the class's singleton class, so it should be defined as an instance method inside the class (not a class method). It receives the method name as a symbol.
Step 2: Check the options
def method_added(method_name); puts "Added: #{method_name}"; end defines def method_added(method_name) which is correct. def self.method_added(method_name); puts "Added: #{method_name}"; end defines it as a class method (self.method_added), which is incorrect. Options B and C miss the required parameter.
Final Answer:
def method_added(method_name); puts "Added: #{method_name}"; end -> Option C
Quick Check:
method_added is an instance method with one parameter [OK]
Hint: Define method_added as instance method with one parameter [OK]
Common Mistakes:
Defining method_added as a class method
Omitting the method_name parameter
Using wrong method signature without parameters
3. What will be the output of the following Ruby code?
class Test
def method_added(name)
puts "Method added: #{name}"
end
def hello
puts 'Hello world'
end
end
Test.new.hello
medium
A. Method added: hello
Hello world
B. Hello world
C. Method added: hello
D. No output
Solution
Step 1: Trace the class definition
During class definition, def method_added is processed first; the hook for defining method_added itself does not print as it is not yet active. Then def hello triggers Test.method_added(:hello), printing "Method added: hello".
Hook triggers for methods defined after method_added [OK]
Hint: method_added hook triggers during class def for subsequent methods [OK]
Common Mistakes:
Thinking method_added as instance method does not hook
Forgetting hook triggers at class definition time
Expecting only "Hello world"
4. Identify the error in this Ruby code using method_added hook:
class Demo
def method_added(name)
puts "Added: #{name}"
def new_method
puts 'New method called'
end
end
def test
puts 'Test method'
end
end
Demo.new.new_method
medium
A. Infinite recursion caused by defining a method inside method_added
B. Syntax error due to missing end keyword
C. No error; code runs and prints 'New method called'
D. new_method is private and cannot be called
Solution
Step 1: Analyze method_added behavior
Inside method_added, a new method new_method is defined. Defining a method triggers method_added again, causing infinite recursion.
Step 2: Understand the recursion effect
This recursion never ends, leading to a SystemStackError (stack overflow) at runtime.
Final Answer:
Infinite recursion caused by defining a method inside method_added -> Option A
By setting a flag (e.g., a class variable) before defining methods inside the hook and checking it at the start of method_added, you can skip the hook logic during internal method definitions.
Step 3: Implement safe tracking
This approach allows tracking new methods safely without recursion.
Final Answer:
Use a guard variable to skip method_added logic when defining methods inside the hook -> Option D
Quick Check:
Guard variable prevents recursion in method_added [OK]
Hint: Use a flag to skip method_added during internal method definitions [OK]
Common Mistakes:
Not using a guard variable and causing recursion
Trying to remove method_added dynamically
Misusing alias_method to fix recursion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.