Method_missing lets your program catch calls to methods that don't exist. It helps handle unexpected or missing actions smoothly.
Method_missing for catch-all in Ruby
def method_missing(method_name, *args, &block) # your code here end
method_name is a symbol of the called method's name.
*args captures all arguments passed to the method.
def method_missing(method_name, *args) puts "You called: #{method_name} with #{args.join(', ')}" end
def method_missing(method_name, *args) if method_name.to_s.start_with?('say_') puts "You asked me to say: #{method_name.to_s[4..]}" else super end end
This program creates a class that catches all unknown method calls and prints their names and arguments.
class CatchAll def method_missing(method_name, *args) puts "Oops! You tried to call '#{method_name}' with arguments: #{args.join(', ')}" end end obj = CatchAll.new obj.hello('world') obj.any_method(1, 2, 3)
Always call super in method_missing if you don't handle the method to avoid hiding bugs.
Using method_missing can make debugging harder if overused, so use it carefully.
method_missing catches calls to methods that don't exist.
It helps create flexible and dynamic behavior in your objects.
Remember to handle only what you expect and call super otherwise.
Practice
What is the main purpose of the method_missing method in Ruby?
Solution
Step 1: Understand what
method_missingdoesmethod_missingis called when an object receives a method call it does not have defined.Step 2: Identify its purpose
It allows handling these unknown method calls dynamically, enabling flexible behavior.Final Answer:
To catch calls to methods that do not exist and handle them dynamically. -> Option AQuick Check:
method_missingcatches unknown methods [OK]
- Thinking method_missing defines new methods automatically
- Confusing method_missing with attr_accessor
- Assuming method_missing overrides existing methods
Which of the following is the correct syntax to define method_missing in a Ruby class?
class Example # Your code here end
Solution
Step 1: Recall method_missing signature
The method receives the missing method's name as a symbol and any arguments as a splat array.Step 2: Check each option's parameters
def method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end correctly usesmethod_name, *argsto capture method name and all arguments.Final Answer:
def method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end -> Option CQuick Check:
Correct parameters = def method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end [OK]
- Forgetting the splat (*) before args
- Using only one parameter for method_missing
- Passing args without splat causing errors
What will be the output of this Ruby code?
class CatchAll
def method_missing(name, *args)
"Called #{name} with #{args.join(', ')}"
end
end
obj = CatchAll.new
puts obj.hello('world')Solution
Step 1: Understand method_missing behavior
The method_missing catches the call tohellowhich is undefined and returns a string with method name and arguments.Step 2: Evaluate the output
Callingobj.hello('world')triggers method_missing with name:helloand args['world'], so output is "Called hello with world".Final Answer:
Called hello with world -> Option BQuick Check:
method_missing output = Called hello with world [OK]
- Expecting NoMethodError instead of method_missing output
- Confusing method name symbol with string
- Ignoring args passed to method_missing
Identify the error in this Ruby class using method_missing:
class Test
def method_missing(name)
puts "Missing: #{name}"
end
end
Test.new.unknown_method(1, 2)Solution
Step 1: Check method_missing parameters
The callunknown_method(1, 2)passes two arguments, but method_missing only accepts one parametername.Step 2: Identify the fix
To handle any number of arguments, method_missing must accept*argsas a splat parameter.Final Answer:
method_missing should accept a splat argument for extra parameters. -> Option AQuick Check:
Missing splat args causes error [OK]
- Ignoring extra arguments passed to missing method
- Assuming puts return is enough
- Thinking method_missing can't be defined in classes
Given this class, what will obj.foo.bar(1) output?
class CatchAll
def method_missing(name, *args)
if name == :bar
"Handled bar with #{args[0]}"
else
self
end
end
end
obj = CatchAll.new
puts obj.foo.bar(1)Solution
Step 1: Trace obj.foo call
Callingobj.footriggers method_missing withname=:foo, which returnsself(the object itself).Step 2: Trace .bar(1) call on returned self
Calling.bar(1)triggers method_missing withname=:barand args[1], which returns "Handled bar with 1".Final Answer:
Handled bar with 1 -> Option DQuick Check:
Chained method_missing returns handled string [OK]
- Expecting NoMethodError on first call
- Not realizing method_missing returns self for chaining
- Assuming args are missing for bar method
Ready to prove it?
Pass the test and this pattern moves unknown
