Bird
Raised Fist0
Rubyprogramming~30 mins

Respond_to_missing? convention in Ruby - Mini Project: Build & Apply

Choose your learning style10 modes available
Using the respond_to_missing? Convention in Ruby
📖 Scenario: Imagine you are building a simple Ruby class that can answer questions about a person, but only for certain known questions. You want your class to behave nicely when Ruby checks if it can respond to a question (method call) that might not be explicitly defined.
🎯 Goal: You will create a Ruby class that uses the respond_to_missing? method to correctly tell Ruby which methods it can handle dynamically. This helps Ruby and other programmers understand what your object can do.
📋 What You'll Learn
Create a class called Person with a constructor that takes a name string
Define a method_missing method that handles the method greet by returning a greeting string
Define a respond_to_missing? method that returns true for the greet method and false otherwise
Create an instance of Person with the name "Alice"
Check if the instance responds to greet and farewell methods
Call the greet method on the instance and print the result
💡 Why This Matters
🌍 Real World
Using <code>respond_to_missing?</code> helps Ruby objects behave predictably when they handle methods dynamically, such as in libraries that create methods on the fly or proxy objects.
💼 Career
Understanding this convention is important for Ruby developers working with metaprogramming, dynamic APIs, or frameworks like Rails that rely on method_missing patterns.
Progress0 / 4 steps
1
Create the Person class with a name attribute
Create a class called Person with an initialize method that takes a parameter name and stores it in an instance variable @name.
Ruby
Hint

Use def initialize(name) and set @name = name inside the method.

2
Add method_missing to handle greet
Inside the Person class, define a method_missing method that takes method_name and *args. If method_name is :greet, return a string "Hello, " followed by the @name. Otherwise, call super.
Ruby
Hint

Check if method_name equals :greet. Return the greeting string or call super.

3
Add respond_to_missing? to declare support for greet
Inside the Person class, define a respond_to_missing? method that takes method_name and include_private. Return true if method_name is :greet, otherwise return super.
Ruby
Hint

Return true if method_name is :greet, else call super.

4
Create instance, check methods, and print greeting
Create a variable person as a new Person with the name "Alice". Print the result of person.respond_to?(:greet) and person.respond_to?(:farewell). Then print the result of calling person.greet.
Ruby
Hint

Create the person object, print respond_to? results for :greet and :farewell, then print person.greet.

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