Challenge - 5 Problems
Method_added Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method_added hook example
What is the output of this Ruby code using the
method_added hook?Ruby
class Test def self.method_added(name) puts "Method added: #{name}" end def hello 'Hello' end def world 'World' end end
Attempts:
2 left
💡 Hint
The
method_added hook is called each time an instance method is defined in the class.✗ Incorrect
The
method_added hook is triggered after each instance method definition. Here, hello and world are instance methods, so the hook prints their names. The hook itself is a class method, so defining it does not trigger method_added.🧠 Conceptual
intermediate1:30remaining
Understanding method_added hook behavior
Which statement about the
method_added hook in Ruby is TRUE?Attempts:
2 left
💡 Hint
Think about what kind of methods
method_added tracks.✗ Incorrect
The
method_added hook is triggered only when an instance method is added to a class, not for class methods or object instantiation.🔧 Debug
advanced2:30remaining
Why does this method_added hook cause a SystemStackError?
Consider this Ruby code snippet:
class Demo
def self.method_added(name)
puts "Added: #{name}"
def new_method
'Hi'
end
end
def first
end
end
What causes the SystemStackError (stack level too deep) when this code runs?Attempts:
2 left
💡 Hint
Think about what happens when a method is defined inside
method_added.✗ Incorrect
Defining a method inside
method_added triggers method_added again, causing infinite recursion and eventually a stack overflow error.📝 Syntax
advanced1:30remaining
Identify the syntax error in method_added hook usage
Which option contains a syntax error in defining a
method_added hook?Attempts:
2 left
💡 Hint
Remember where
method_added must be defined to work as a hook.✗ Incorrect
The
method_added hook must be defined as a class method (using self.method_added). Option D defines it as an instance method, so it won't work as a hook but is syntactically valid. However, the question asks for syntax error, so check string interpolation carefully.🚀 Application
expert3:00remaining
Using method_added to track method definitions
You want to track all instance methods added to a class and store their names in an array
@methods_list. Which code snippet correctly implements this using method_added?Attempts:
2 left
💡 Hint
Remember that
@methods_list is a class instance variable and may need initialization inside the hook.✗ Incorrect
Option A correctly initializes
@methods_list if nil inside method_added and appends method names. This avoids errors if the hook runs before @methods_list is set. Other options either define method_added incorrectly or overwrite the array.