Bird
Raised Fist0
Rubyprogramming~10 mins

Respond_to_missing? convention in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Respond_to_missing? convention
Call to respond_to? method
↓
Check normal methods
↓
Call respond_to_missing?
↓
Return true/false
↓
Return true
↓
End
When Ruby checks if an object responds to a method, it first looks for normal methods. If none found, it calls respond_to_missing? to decide if it can handle the method dynamically.
Execution Sample
Ruby
class MyClass
  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s.start_with?('dynamic_')
  end
end
This code defines respond_to_missing? to say the object responds to methods starting with 'dynamic_'.
Execution Table
StepMethod CalledNormal Method Found?Call respond_to_missing?respond_to_missing? ResultFinal respond_to? Result
1dynamic_testNoYestruetrue
2normal_methodYesNoN/Atrue
3unknown_methodNoYesfalsefalse
4dynamic_helloNoYestruetrue
5to_sYesNoN/Atrue
💡 Execution stops after respond_to? returns true or false based on method presence or respond_to_missing? result.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
method_namenildynamic_testnormal_methodunknown_methoddynamic_helloto_s
normal_method_foundfalsefalsetruefalsefalsetrue
respond_to_missing_calledfalsetruefalsetruetruefalse
respond_to_missing_resultniltrueN/AfalsetrueN/A
respond_to_resultniltruetruefalsetruetrue
Key Moments - 3 Insights
Why does respond_to? call respond_to_missing? when no normal method is found?
Because respond_to_missing? lets objects say they can handle methods dynamically, so respond_to? checks it to be accurate (see rows 1, 3, 4 in execution_table).
What happens if respond_to_missing? returns false?
Then respond_to? returns false, meaning the object does not respond to that method (see step 3 in execution_table).
Does respond_to? return true immediately if a normal method exists?
Yes, if a normal method is found, respond_to? returns true without calling respond_to_missing? (see step 2 and 5 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the respond_to? result when method_name is 'dynamic_test'?
Atrue
Bfalse
CN/A
Draises error
💡 Hint
Check row 1 in execution_table for 'dynamic_test' method call and final respond_to? result.
At which step does respond_to_missing? return false?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'respond_to_missing? Result' column in execution_table.
If a normal method is found, what does respond_to? do next?
ACalls respond_to_missing?
BReturns true immediately
CReturns false
DRaises an error
💡 Hint
See steps 2 and 5 in execution_table where normal_method_found is Yes.
Concept Snapshot
respond_to? checks if an object has a method.
If no normal method, it calls respond_to_missing?.
respond_to_missing? returns true if object can handle method dynamically.
Always override respond_to_missing? when using method_missing.
This keeps respond_to? accurate and consistent.
Full Transcript
In Ruby, when you ask an object if it responds to a method using respond_to?, Ruby first looks for a normal method. If it doesn't find one, it calls respond_to_missing? to check if the object can handle the method dynamically. This is important when using method_missing to catch calls to undefined methods. The respond_to_missing? method should return true if the object can handle the method name, or false otherwise. This way, respond_to? stays accurate. For example, if respond_to_missing? returns true for methods starting with 'dynamic_', respond_to? will also return true for those methods even if they are not defined normally. If a normal method exists, respond_to? returns true immediately without calling respond_to_missing?. If respond_to_missing? returns false, respond_to? returns false, meaning the object does not respond to that method. This convention helps Ruby objects behave predictably when handling dynamic methods.

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