Complete the code to define a method that prints a message when a new method is added.
class MyClass def self.method_added([1]) puts "New method added: #{method_name}" end def greet puts 'Hello!' end end
The method_added hook receives the name of the newly added method as a symbol. Naming the parameter method_name is clear and conventional.
Complete the code to print the name of each method added to the class.
class Tracker def self.method_added([1]) puts "Method added: #{method_name}" end def example_method end end
The parameter method_name correctly receives the symbol of the method just added, which is then printed.
Fix the error in the method_added hook to correctly print the added method's name.
class Fixer def self.method_added([1]) puts "Added method: #{method_name}" end def test end end
The parameter method_name matches the variable used inside the method, fixing the error.
Fill both blanks to create a method_added hook that ignores methods starting with an underscore.
class IgnoreUnderscore def self.method_added([1]) return if method_name.to_s.start_with?([2]) puts "Added: #{method_name}" end def _hidden end def visible end end
The parameter method_name receives the method's name. Checking if it starts with '_' allows ignoring such methods.
Fill all three blanks to create a method_added hook that tracks added methods in a class variable array.
class Tracker @methods = [] def self.method_added([1]) @methods [2] [3] end def first end def second end end
+= which creates a new array instead of modifying the existing one.The hook receives the method name as method_name. Using << adds it to the @methods array.