In Ruby frameworks, hooks allow developers to insert custom code at specific points. Why is this useful for framework building?
Think about how frameworks allow users to add their own behavior without modifying the framework itself.
Hooks provide predefined spots where user code can run, so frameworks stay flexible and easy to extend.
What is the output of this Ruby code using a hook method?
class Framework def initialize @hooks = [] end def add_hook(&block) @hooks << block end def run puts "Start" @hooks.each(&:call) puts "End" end end fw = Framework.new fw.add_hook { puts "Hook 1" } fw.add_hook { puts "Hook 2" } fw.run
Look at the order of method calls and when hooks are called.
The framework prints "Start", then calls each hook in order, then prints "End".
What error does this Ruby code raise when trying to add a hook?
class Framework def initialize @hooks = nil end def add_hook(&block) @hooks << block end end fw = Framework.new fw.add_hook { puts "Hello" }
Check the initial value of @hooks and what << means.
@hooks is nil, so calling << on nil causes NoMethodError.
Which of these options correctly defines a method to add a hook block to an array in Ruby?
Remember how to accept a block and store it without calling it.
Option D correctly accepts a block and stores it in @hooks without calling it.
In an event-driven Ruby framework, hooks allow user code to run when events happen. Which statement best explains this?
Think about how frameworks notify user code when something happens.
Hooks let frameworks call user code automatically on events, making the framework adaptable and extensible.