Bird
Raised Fist0
Rubyprogramming~10 mins

Respond_to_missing? convention in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a method that checks if an object responds to a method name.

Ruby
def respond_to_missing?(method_name, include_private = false)
  method_name == :[1]
end
Drag options to blanks, or click blank then click option'
Abaz
Bbar
Cqux
Dfoo
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a symbol for method_name comparison.
Checking for the wrong method name symbol.
2fill in blank
medium

Complete the code to properly override respond_to_missing? to handle a dynamic method.

Ruby
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('find_') || [1]
end
Drag options to blanks, or click blank then click option'
Afalse
Bsuper
Ctrue
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Returning true or false directly without calling super.
Using nil which is not a boolean.
3fill in blank
hard

Fix the error in the method_missing implementation to correctly handle missing methods.

Ruby
def method_missing(method_name, *args, &block)
  if method_name.to_s.start_with?('find_')
    "Finding record for #{method_name}"
  else
    [1]
  end
end
Drag options to blanks, or click blank then click option'
Aputs 'Error'
Breturn nil
Csuper
Draise ArgumentError
Attempts:
3 left
💡 Hint
Common Mistakes
Returning nil silently hides errors.
Raising the wrong exception type.
Printing errors instead of raising.
4fill in blank
hard

Fill both blanks to complete a class that uses respond_to_missing? and method_missing to handle dynamic methods starting with 'get_'.

Ruby
class DynamicGetter
  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s.start_with?([1]) || super
  end

  def method_missing(method_name, *args, &block)
    if method_name.to_s.start_with?([2])
      "Got #{method_name}"
    else
      super
    end
  end
end
Drag options to blanks, or click blank then click option'
A'get_'
B'set_'
C'find_'
D'fetch_'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different prefixes in the two methods.
Forgetting to call super in either method.
5fill in blank
hard

Fill all three blanks to complete a class that dynamically responds to methods starting with 'calculate_', returning the calculation name.

Ruby
class Calculator
  def respond_to_missing?(method_name, include_private = false)
    method_name.to_s.start_with?([1]) || super
  end

  def method_missing(method_name, *args, &block)
    if method_name.to_s.start_with?([2])
      "Calculating #{method_name.to_s[[3]..]}"
    else
      super
    end
  end
end
Drag options to blanks, or click blank then click option'
A'calculate_'
B'compute_'
C10
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Using different prefixes in the two methods.
Incorrect slice index causing wrong substring.
Not calling super in fallback cases.

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