Bird
Raised Fist0
RubyHow-ToBeginner · 3 min read

How to Use respond_to_missing in Ruby: Syntax and Examples

In Ruby, respond_to_missing? is used alongside method_missing to correctly indicate if an object can respond to a method dynamically. You override respond_to_missing? to return true for methods handled by method_missing, ensuring respond_to? works as expected.
📐

Syntax

The respond_to_missing? method takes two parameters: method_name (a symbol of the method being checked) and include_private (a boolean indicating if private methods should be considered). It should return true if the object can handle the method dynamically, otherwise false.

ruby
def respond_to_missing?(method_name, include_private = false)
  # return true if method_name is handled dynamically
  false
end
💻

Example

This example shows a class that handles any method starting with say_ dynamically using method_missing. The respond_to_missing? method is overridden to return true for those methods, so respond_to? works correctly.

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

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

g = Greeter.new
puts g.say_hello("world")
puts g.respond_to?(:say_hello)
puts g.respond_to?(:unknown_method)
Output
You called: say_hello with arguments: world true false
⚠️

Common Pitfalls

  • Not overriding respond_to_missing? when using method_missing causes respond_to? to return false even if the method is handled dynamically.
  • Forgetting to call super in respond_to_missing? can break behavior for methods not handled dynamically.
  • Returning true incorrectly for methods not handled can cause unexpected behavior.
ruby
class BadExample
  def method_missing(method_name, *args)
    "Handled dynamically"
  end

  def respond_to_missing?(method_name, include_private = false)
    true # Incorrect: claims to respond to all methods
  end
end

b = BadExample.new
puts b.respond_to?(:any_method) # true, but might not handle properly

class GoodExample
  def method_missing(method_name, *args)
    if method_name == :dynamic_method
      "Handled dynamically"
    else
      super
    end
  end

  def respond_to_missing?(method_name, include_private = false)
    method_name == :dynamic_method || super
  end
end

g = GoodExample.new
puts g.respond_to?(:dynamic_method) # true
puts g.respond_to?(:other_method)   # false
Output
true true true false
📊

Quick Reference

  • Purpose: Make respond_to? aware of methods handled by method_missing.
  • Parameters: method_name (Symbol), include_private (Boolean, default false).
  • Return: true if method is handled dynamically, else false.
  • Best Practice: Always call super for methods not handled.
✅

Key Takeaways

Override respond_to_missing? when using method_missing to keep respond_to? accurate.
Always call super in respond_to_missing? for methods you don't handle dynamically.
Use respond_to_missing? to check method names your object can handle dynamically.
Incorrect respond_to_missing? can cause bugs in method detection and introspection.
respond_to_missing? improves Ruby objects' compatibility with reflection and dynamic calls.