0
0
Rubyprogramming~20 mins

Method_added hook in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Method_added Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A
Method added: hello
Method added: world
B
Method added: hello
Method added: world
Method added: method_added
CNo output
DMethod added: hello
Attempts:
2 left
💡 Hint
The method_added hook is called each time an instance method is defined in the class.
🧠 Conceptual
intermediate
1:30remaining
Understanding method_added hook behavior
Which statement about the method_added hook in Ruby is TRUE?
AIt is called every time a class method is defined.
BIt is called every time an instance method is defined.
CIt is called only once when the class is created.
DIt is called when an instance of the class is created.
Attempts:
2 left
💡 Hint
Think about what kind of methods method_added tracks.
🔧 Debug
advanced
2: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?
AThe <code>new_method</code> is defined inside an instance method, which is invalid.
BThe <code>method_added</code> hook is missing a <code>super</code> call.
CThe <code>method_added</code> hook defines a new method, which triggers <code>method_added</code> again, causing infinite recursion.
DThe <code>first</code> method is empty, causing the error.
Attempts:
2 left
💡 Hint
Think about what happens when a method is defined inside method_added.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in method_added hook usage
Which option contains a syntax error in defining a method_added hook?
A
def self.method_added(name)
  puts 'Added: ' + name
end
B
def self.method_added(name)
  puts "Added: #{name}"
end
C
dne
"}eman{# :deddA" stup  
)eman(dedda_dohtem.fles fed
D
def method_added(name)
  puts "Added: #{name}"
end
Attempts:
2 left
💡 Hint
Remember where method_added must be defined to work as a hook.
🚀 Application
expert
3: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?
A
class Tracker
  @methods_list = []
  def self.method_added(name)
    @methods_list = [] unless @methods_list
    @methods_list.push(name)
  end
  def self.methods_list
    @methods_list
  end
  def foo; end
  def bar; end
end

puts Tracker.methods_list.inspect
B
class Tracker
  @methods_list = []
  def self.method_added(name)
    @methods_list &lt;&lt; name
  end
  def self.methods_list
    @methods_list
  end
  def foo; end
  def bar; end
end

puts Tracker.methods_list.inspect
C
class Tracker
  @methods_list = []
  def method_added(name)
    @methods_list &lt;&lt; name
  end
  def self.methods_list
    @methods_list
  end
  def foo; end
  def bar; end
end

puts Tracker.methods_list.inspect
D
class Tracker
  @methods_list = []
  def self.method_added(name)
    @methods_list &lt;&lt; name
  end
  def self.methods_list
    @methods_list
  end
  def foo; end
  def bar; end
  def self.method_added(name)
    @methods_list &lt;&lt; name
  end
end

puts Tracker.methods_list.inspect
Attempts:
2 left
💡 Hint
Remember that @methods_list is a class instance variable and may need initialization inside the hook.