method_missing done right
The pair
method_missing is called when the lookup fails. Overriding it handles a name the class does not define, and respond_to_missing? answers the matching question for respond_to?. They are a pair: one without the other produces an object that lies about what it can do.
class Config def method_missing(name, *args) return super unless name.to_s.end_with?("=") || args.empty? @values ||= {} if name.to_s.end_with?("=") @values[name.to_s.chomp("=")] = args.first else @values[name.to_s] end end def respond_to_missing?(name, include_private = false) name.to_s.end_with?("=") || (@values || {}).key?(name.to_s) || super end end
super in both is the part that keeps Ruby's error messages. A method_missing that never calls super turns every typo into silence.
A settings object
A settings hash with attribute-style access is the small case that earns the pair. The whole file repeats the class so it runs alone; reading and writing work, and an unknown call with an argument still raises:
class Config def method_missing(name, *args) return super unless name.to_s.end_with?("=") || args.empty? @values ||= {} if name.to_s.end_with?("=") @values[name.to_s.chomp("=")] = args.first else @values[name.to_s] end end def respond_to_missing?(name, include_private = false) name.to_s.end_with?("=") || (@values || {}).key?(name.to_s) || super end end config = Config.new config.color = "red" p config.color p config.respond_to?(:color) p config.respond_to?(:nope) begin config.nope(1) rescue NoMethodError => e puts "NoMethodError: #{e.message}" end
ruby missing.rb
"red" true false NoMethodError: undefined method 'nope' for an instance of Config
The call with an argument goes to super because the guard rejects it, so Ruby builds the usual message. The respond_to? answers are the ones a framework or a serialiser checks before it calls.
respond_to_missing?
Ruby calls respond_to_missing? only after the normal lookup fails, and only from respond_to?. It must not raise, and it must accept the second argument:
def respond_to_missing?(name, include_private = false) name.to_s.end_with?("=") || (@values || {}).key?(name.to_s) || super end
The method is a promise: when it answers true, a caller may call the name and expect method_missing to handle it. A promise that is broader than the handler produces a NoMethodError from code that checked first.
When it is wrong
- A fixed set of names. If the names are known, define them with define_method (next lesson). Then methods, tab completion and the documentation all see them.
- A typo. A catch-all handler turns a misspelled method into a nil that travels three frames. The super in the pair is what prevents this; a handler without it is a bug.
- A public API. A method that exists only through method_missing is invisible to a reader and to the editor. Reserve the trick for a proxy, a configuration object or a compatibility layer with a documented list.
What to watch for
method_missing on Object
changes every object in the process. A library that does it is a global
change, and two such libraries can disagree about a name.
A handler that is slow is called for every missing name, including the
ones from inspect and
respond_to? probes. Keep it to a hash lookup and
let super do the rest.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
class Config def method_missing(name, *args) return super unless name.to_s.end_with?("=") || args.empty? @values ||= {} if name.to_s.end_with?("=") @values[name.to_s.chomp("=")] = args.first else @values[name.to_s] end end end config = Config.new config.color = "red" puts config.colorAnswer
red
The setter stores the value under color, and the getter reads it back by the same name.
-
What does this program print?
class Config def method_missing(name, *args) return super unless name.to_s.end_with?("=") || args.empty? @values ||= {} if name.to_s.end_with?("=") @values[name.to_s.chomp("=")] = args.first else @values[name.to_s] end end def respond_to_missing?(name, include_private = false) name.to_s.end_with?("=") || (@values || {}).key?(name.to_s) || super end end config = Config.new config.color = "red" puts config.respond_to?(:color) puts config.respond_to?(:nope)Answer
true / false
respond_to_missing? answers true only for a name the handler really serves, which is the promise the pair makes.
-
What does this program print?
class Config def method_missing(name, *args) return super unless name.to_s.end_with?("=") || args.empty? @values ||= {} if name.to_s.end_with?("=") @values[name.to_s.chomp("=")] = args.first else @values[name.to_s] end end end puts Config.new.nope.inspectAnswer
nil
A missing getter with no arguments reaches the handler, which answers nil for a key it has not stored.
-
What goes wrong when a class overrides
method_missingwithoutsuper?Answer
Every typo returns nil instead of raising the usual NoMethodError
A catch-all without super turns a misspelled method into a nil that travels through the program.
-
Why must
method_missingandrespond_to_missing?agree?Answer
A caller that checks respond_to? then calls the name expects the handler to serve it
A broader promise than the handler produces a NoMethodError in code that checked first.
-
When should a fixed set of dynamic names be defined with
define_methodinstead?Answer
When the names are known, so methods, tab completion and documentation see them
A real method is visible to every tool, while a method_missing name is invisible until it is called.
-
Why is overriding
method_missingon Object a global change?Answer
It changes the lookup for every object in the process
Every object inherits from Object, so one handler answers for names the whole process misses.
-
Why keep a
method_missinghandler to a hash lookup?Answer
It is called for every missing name, including probes from inspect and respond_to?
The handler sits on the slow path of every miss, so heavy work there costs every probe.
-
Why does
respond_to_missing?take a second argument?Answer
It says whether private names count, and the method must accept it
respond_to? passes the include_private flag, and a signature that refuses it breaks the call.
-
Which case does the page call a wrong use of the pair?
Answer
A public API whose methods exist only through method_missing
A name that is invisible to a reader and an editor does not belong in a public API.
Article author: Arthur Isaev