Complete the code to define a method that catches calls to undefined methods.
def [1](method_name) puts "You called #{method_name}!" end
The method_missing method in Ruby is called when an undefined method is invoked on an object.
Complete the code to print the name of the missing method and its arguments.
def method_missing(method_name, *[1]) puts "Called #{method_name} with #{args.inspect}" end
The splat operator *args collects all arguments passed to the missing method into an array named args.
Fix the error in the method_missing definition to correctly handle missing methods with arguments.
def method_missing(method_name, [1]) puts "Missing method: #{method_name}" end
The splat operator *args is needed to capture any number of arguments passed to the missing method.
Fill both blanks to define method_missing that prints method name and block presence.
def method_missing(method_name, *args, &[1]) if [2] puts "Block given to #{method_name}" else puts "No block given to #{method_name}" end end
The &block captures an optional block passed to the method. The block_given? method checks if a block was provided.
Fill all three blanks to define method_missing that logs method name, arguments, and returns a default message.
def method_missing([1], *[2], &block) puts "Called #{method_name} with #{args.inspect}" "Default response for #[3]" end
The method parameters are method_name for the method called, *args for arguments, and &block for an optional block. The method returns a default string including the method name.