Bird
Raised Fist0
Rubyprogramming~20 mins

Respond_to_missing? convention in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Respond_to_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 respond_to_missing?

Consider this Ruby class that uses method_missing and respond_to_missing?. What will be printed when calling obj.greet?

Ruby
class Greeter
  def respond_to_missing?(method_name, include_private = false)
    method_name == :greet || super
  end

  def method_missing(method_name, *args, &block)
    if method_name == :greet
      "Hello!"
    else
      super
    end
  end
end

obj = Greeter.new
puts obj.greet
AHello!
BNoMethodError
Cfalse
Dnil
Attempts:
2 left
💡 Hint

Check how respond_to_missing? affects method detection and how method_missing handles calls.

🧠 Conceptual
intermediate
1:30remaining
Why implement respond_to_missing? when using method_missing?

Why is it important to implement respond_to_missing? when you override method_missing in Ruby?

ATo automatically define missing methods without writing them manually.
BTo speed up method calls by caching missing methods.
CTo correctly report which methods the object can respond to, improving compatibility with Ruby's introspection methods.
DTo prevent runtime errors when calling any undefined method.
Attempts:
2 left
💡 Hint

Think about how Ruby checks if an object responds to a method before calling it.

🔧 Debug
advanced
2:00remaining
What error does this code raise and why?

Examine this Ruby code snippet. What error will it raise when calling obj.foo and why?

Ruby
class Speaker < BasicObject
  def respond_to_missing?(method_name, include_private = false)
    method_name == :hello
  end

  def method_missing(method_name, *args, &block)
    if method_name == :hello
      "Hi!"
    else
      super
    end
  end
end

obj = Speaker.new
puts obj.foo
ANoMethodError: super called in method_missing without a superclass method
BHello!
CArgumentError: wrong number of arguments
DNameError: undefined local variable or method
Attempts:
2 left
💡 Hint

Check what happens when super is called inside method_missing without a parent method_missing.

📝 Syntax
advanced
2:00remaining
Which option correctly implements respond_to_missing? for dynamic methods?

Which of the following Ruby code snippets correctly implements respond_to_missing? to handle dynamic methods starting with 'find_'?

A
def respond_to_missing?(method_name, include_private = false)
  method_name.start_with?("find_")
end
B
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?("find_") || super
end
C
def respond_to_missing?(method_name)
  method_name.start_with?("find_") || super
end
D
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.include?("find_") || super
end
Attempts:
2 left
💡 Hint

Remember the method signature and that method_name is a symbol.

🚀 Application
expert
2:30remaining
How many methods does this object respond to?

Given this Ruby class, how many methods will obj.respond_to? return true for, when checking :foo1, :foo2, :bar, and :baz?

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

  def method_missing(method_name, *args, &block)
    if method_name.to_s.start_with?("foo")
      "Handled #{method_name}"
    else
      super
    end
  end
end

obj = DynamicResponder.new
results = {
  foo1: obj.respond_to?(:foo1),
  foo2: obj.respond_to?(:foo2),
  bar: obj.respond_to?(:bar),
  baz: obj.respond_to?(:baz)
}
puts results.select { |_, v| v }.size
A3
B4
C0
D2
Attempts:
2 left
💡 Hint

Check which method names start with 'foo' and how respond_to_missing? works.

Practice

(1/5)
1.

What is the main purpose of respond_to_missing? in Ruby?

easy
A. To tell Ruby if an object can handle a method not explicitly defined
B. To automatically define missing methods
C. To raise errors when a method is missing
D. To list all methods of an object

Solution

  1. Step 1: Understand the role of respond_to_missing?

    This method is used to inform Ruby whether an object can respond to a method that is not explicitly defined in the class.
  2. Step 2: Differentiate from other options

    It does not define methods automatically, raise errors, or list methods; it only answers if a method can be handled dynamically.
  3. Final Answer:

    To tell Ruby if an object can handle a method not explicitly defined -> Option A
  4. Quick Check:

    respond_to_missing? = informs about dynamic methods [OK]
Hint: Remember: respond_to_missing? answers if method can be handled [OK]
Common Mistakes:
  • Thinking it defines missing methods automatically
  • Confusing it with method_missing itself
  • Assuming it raises errors for missing methods
2.

Which of the following is the correct way to define respond_to_missing? in a Ruby class?

class MyClass
  def respond_to_missing?(method_name, include_private = false)
    # ???
  end
end
easy
A. return method_name == :find_user
B. return true if method_name.to_s.start_with?("find_")
C. return false
D. return method_missing(method_name)

Solution

  1. Step 1: Check the method signature and expected return

    respond_to_missing? should return true or false depending on whether the object can handle the method dynamically.
  2. Step 2: Analyze each option

    return true if method_name.to_s.start_with?("find_") returns true if the method name starts with "find_", which is a common pattern for dynamic methods. return method_name == :find_user only returns true for one exact method, which is valid but less flexible. return false always returns false, which defeats the purpose. return method_missing(method_name) incorrectly calls method_missing, which is not correct here.
  3. Final Answer:

    return true if method_name.to_s.start_with?("find_") -> Option B
  4. Quick Check:

    respond_to_missing? returns true for dynamic method patterns [OK]
Hint: Use method_name.to_s and return true for matching patterns [OK]
Common Mistakes:
  • Calling method_missing inside respond_to_missing?
  • Always returning false
  • Checking method_name without converting to string
3.

What will be the output of the following Ruby code?

class Dynamic
  def respond_to_missing?(method_name, _)
    method_name.to_s.start_with?("say_")
  end

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

d = Dynamic.new
puts d.say_hello("world")
puts d.unknown_method
medium
A. You called say_hello with world NoMethodError
B. NoMethodError NoMethodError
C. You called say_hello with world You called unknown_method with
D. No output

Solution

  1. Step 1: Analyze respond_to_missing? behavior

    It returns true only for methods starting with "say_".
  2. Step 2: Trace method calls

    d.say_hello("world") triggers method_missing, which checks respond_to_missing? and returns true, so it returns the string. d.unknown_method triggers method_missing, respond_to_missing? returns false, so super is called, raising NoMethodError.
  3. Final Answer:

    You called say_hello with world NoMethodError -> Option A
  4. Quick Check:

    respond_to_missing? controls method_missing handling [OK]
Hint: respond_to_missing? true means method_missing handles it [OK]
Common Mistakes:
  • Assuming all missing methods are handled
  • Ignoring super call in method_missing
  • Not matching method_name pattern correctly
4.

Identify the error in this Ruby class using respond_to_missing? and method_missing:

class Broken
  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s.start_with?("do_")
  end

  def method_missing(method_name, *args)
    "Doing #{method_name}"
  end
end

b = Broken.new
puts b.do_something
puts b.unknown
medium
A. respond_to_missing? must not have default parameters
B. respond_to_missing? should always return true
C. method_missing should call super for unknown methods
D. method_missing must not accept *args

Solution

  1. Step 1: Understand method_missing behavior

    method_missing should handle only methods that respond_to_missing? returns true for; otherwise, it should call super to raise NoMethodError.
  2. Step 2: Identify the problem in the code

    Here, method_missing always returns a string, even for unknown methods, so it hides errors and breaks expected behavior.
  3. Final Answer:

    method_missing should call super for unknown methods -> Option C
  4. Quick Check:

    method_missing must call super if method not handled [OK]
Hint: Always call super in method_missing for unhandled methods [OK]
Common Mistakes:
  • Not calling super in method_missing
  • Returning wrong types from method_missing
  • Misusing respond_to_missing? signature
5.

You want to create a Ruby class that dynamically handles methods starting with calc_ by returning the method name reversed. Which respond_to_missing? and method_missing implementation correctly follows the convention?

hard
A. def respond_to_missing?(method_name, _) method_name.to_s.start_with?("calc_") end def method_missing(method_name, *args) method_name.reverse end
B. def respond_to_missing?(method_name, _) true end def method_missing(method_name, *args) method_name.to_s.reverse end
C. def respond_to_missing?(method_name, _) method_name.to_s.include?("calc_") end def method_missing(method_name, *args) method_name.reverse end
D. def respond_to_missing?(method_name, _) method_name.to_s.start_with?("calc_") end def method_missing(method_name, *args) if respond_to_missing?(method_name, false) method_name.to_s.reverse else super end end

Solution

  1. Step 1: Verify respond_to_missing? logic

    The correct version returns true only if the method_name.to_s starts with "calc_" and ignores the include_private parameter using _.
  2. Step 2: Verify method_missing logic

    The correct version checks respond_to_missing?(method_name, false) before returning method_name.to_s.reverse, and calls super otherwise.
  3. Final Answer:

    def respond_to_missing?(method_name, _) method_name.to_s.start_with?("calc_") end def method_missing(method_name, *args) if respond_to_missing?(method_name, false) method_name.to_s.reverse else super end end -> Option D
  4. Quick Check:

    Use respond_to_missing? check and call super in method_missing [OK]
Hint: Call super in method_missing unless respond_to_missing? true [OK]
Common Mistakes:
  • Not calling super in method_missing
  • Using include? instead of start_with?
  • Always returning true in respond_to_missing?
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard