0
0
Rubyprogramming~20 mins

Method_missing for catch-all in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Method Missing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using method_missing?

Consider this Ruby class that uses method_missing to catch undefined method calls:

class CatchAll
  def method_missing(name, *args)
    "Called #{name} with #{args.join(", ")}"
  end
end

obj = CatchAll.new
puts obj.any_method(1, 2, 3)

What will this print?

Ruby
class CatchAll
  def method_missing(name, *args)
    "Called #{name} with #{args.join(", ")}"
  end
end

obj = CatchAll.new
puts obj.any_method(1, 2, 3)
ACalled any_method with 1, 2, 3
BCalled any_method with []
CNoMethodError: undefined method `any_method' for #<CatchAll:0x000...>
DArgumentError: wrong number of arguments (given 3, expected 0)
Attempts:
2 left
💡 Hint

Think about what method_missing does when a method is not found.

🧠 Conceptual
intermediate
1:30remaining
Which statement about method_missing is true?

Choose the correct statement about Ruby's method_missing method.

A<code>method_missing</code> automatically defines the missing method permanently.
B<code>method_missing</code> is called only when a method is not defined on the object.
C<code>method_missing</code> is called before any method is executed, even if it exists.
D<code>method_missing</code> can only handle methods without arguments.
Attempts:
2 left
💡 Hint

Think about when Ruby decides to call method_missing.

🔧 Debug
advanced
2:30remaining
Why does this method_missing code cause a SystemStackError?

Look at this Ruby code:

class RecursiveCatch
  def method_missing(name, *args)
    method_missing(name, *args)
  end
end

obj = RecursiveCatch.new
obj.any_method

Why does this cause a SystemStackError?

ABecause <code>method_missing</code> is not defined with the correct parameters.
BBecause Ruby does not allow <code>method_missing</code> to be called explicitly.
CBecause <code>method_missing</code> calls itself recursively without a base case.
DBecause <code>any_method</code> is actually defined and causes conflict.
Attempts:
2 left
💡 Hint

Think about what happens when method_missing calls itself.

📝 Syntax
advanced
2:00remaining
Which option correctly overrides method_missing to handle undefined methods?

Choose the correct Ruby code snippet that overrides method_missing to print the missing method name and arguments.

A
def method_missing(name, *args)
  puts "Missing: #{name} with #{args.join(', ')}"
end
B
def method_missing(name, args)
  puts "Missing: #{name} with #{args.join(', ')}"
end
C
def method_missing(*name, args)
  puts "Missing: #{name} with #{args.join(', ')}"
end
D
def method_missing(name)
  puts "Missing: #{name} with no args"
end
Attempts:
2 left
💡 Hint

Remember the correct parameters for method_missing.

🚀 Application
expert
3:00remaining
What is the output of this Ruby code using method_missing and respond_to_missing??

Examine this Ruby class:

class DynamicResponder
  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s.start_with?("say_") || super
  end

  def method_missing(method_name, *args)
    if method_name.to_s.start_with?("say_")
      "You called #{method_name} with #{args.join(", ")}"
    else
      super
    end
  end
end

obj = DynamicResponder.new
puts obj.say_hello("world")
puts obj.unknown_method

What will be the output?

Ruby
class DynamicResponder
  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s.start_with?("say_") || super
  end

  def method_missing(method_name, *args)
    if method_name.to_s.start_with?("say_")
      "You called #{method_name} with #{args.join(", ")}"
    else
      super
    end
  end
end

obj = DynamicResponder.new
puts obj.say_hello("world")
puts obj.unknown_method
A
You called say_hello with world
nil
B
You called say_hello with world
You called unknown_method with 
C
NoMethodError: undefined method `say_hello' for #&lt;DynamicResponder:0x000...&gt;
NoMethodError: undefined method `unknown_method' for #&lt;DynamicResponder:0x000...&gt;
D
You called say_hello with world
Traceback (most recent call last): ... NoMethodError: undefined method `unknown_method' for #&lt;DynamicResponder:0x000...&gt;
Attempts:
2 left
💡 Hint

Think about how respond_to_missing? and method_missing work together.