0
0
Rubyprogramming~20 mins

Respond_to_missing? convention in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 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.