10 things you should know about method_missing
[update, 9:27 a.m., 8/1/2007: welcome, reddit readers. annoyed at me for calling this post “10 things”? Well, then you’ll really hate me after reading this post!]
1. method_missing is a Ruby kernel method and everyone should know about it.
2. Rails implements some of its funkiest magic with method_missing. When you ask your model to find_by_freaky_column_name, and it does so, that’s because ActiveRecord::Base overrides the kernel’s method_missing method.
3. method_missing is the method of last resort. When you send a message to a Ruby object, Ruby looks for a method to invoke with the same name as the message you sent. (There are a bunch of different ways to send the message, but the most common one is just obj.method_name. But you can make the fact that you are sending a message explicit by using obj.send(:method_name)). First it looks in the current self object’s own instance methods. Then it looks in the list of instance methods that all objects of that class share, and then in each of the included modules of that class, in reverse order of inclusion. Then it looks in that class’s superclass, and then in the superclass’s included modules, all the way up until it reaches the class Object. If it still can’t find a method, the very last place it looks is in the Kernel module, included in the class Object. And there, if it comes up short, it calls method_missingYou can override method_missing anywhere along that method lookup path, and tell Ruby what to do when it can’t find a method.
4. Sometimes people use method_missing to implement some default behavior they want to occur; more often they seem to use it as a kind of method factory or dispatcher. They have a class or module that they want to have many methods that do generally the same kinds of things, they’re not sure in advance which methods people will actually want to call, or there are so many of them, and they’re all so similar, that it seems like a waste to implement them all by hand. So they just say, okay, if someone gives me a method that doesn’t exist, I’ll assume that they’re trying to do this general kind of thing, and I’ll make them a method based on their name that does the thing they probably want it to do.Sometimes they make a new method and add it to the class (factory); sometimes they just do what needs to be done in method_missing itself (dispatcher).
5. Basically, method_missing is one of the features of Ruby that make it remarkably easy to create your own DSL for fun and profit. DSLs are Domain Specific Languages, which, if you didn’t know — and of course, dear reader, you did, because you are oh-so-up on all the oh-so-fashionable programming paradigms, are the big thing that Martin Fowler is talking about a lot these days.
6. Here’s the kernel’s method_missing implementation. It’s in C, so I have no freakin’ clue what it’s doing, but I know what the result is: a NoMethod error:
irb(main):227:0> "thingy".invoke_method_missing_for_demo_purposes("arg", "other arg")
NoMethodError: undefined method `invoke_method_missing_for_demo_purposes' for "thingy":String from (irb):227
7. Now here’s Jamis Buck showing us how ActiveRecord::Base overrides method_missing. It’s a long but fascinating article that takes you step-by-step through some Rails source code. Go look at it now.
8. method_missing is said to be slower than calling an actually-already existing method, unsurprisingly, given that it’s by definition invoked only after Ruby’s looked everywhere else for your method. The speed issue may or may not matter for your intended use, and my impression is that it usually doesn’t. #{usual disclaimer about benchmarking, using profilers, and not prematurely optimizing your code.}
9. method_missing can also be a bit opaque to users of your code, since dynamically created methods are, of course, not documented in the API.
10. The use of method_missing falls under the general technique of metaprogramming, which is a big deal in Ruby, and not something ordinary folks ever fool around with in many other languages (like Java). At least, I never fooled around with it in Java. But hey, what do I know? What is metaprogramming? C’mon, people, it’s right up there with DSLs in the list of things the kewl kids know. But actually, it really is pretty cool, and it really is pretty much the way Ruby works. And it’s not as difficult as (but waaaay more useful than) say, Lacan, despite the poststructuralist-sounding name and the fact that the metaprogramming wikipedia article makes it sound very fancy.
Ignore the wikipedia article, listen to _why, in a discussion of method_missing: “I don’t think the idea here is to save memory or speed. The idea behind metaprogramming is to teach Ruby your conventions and let it do some guessing, in order to save you some code.”
Don’t trust that _why and all his chunky bacon? Here’s David Black, equally reassuring:
One of the great accomplishments of Ruby is that its object and class model bring about a wonderful unity and simplicity. Even things that might seem like they should be hard or obscure are actually very transparent, once you have the hang of how the model works.Unfortunately, there’s a trend toward drawing an increasingly sharp line between regular programming and “metaprogramming” in Ruby discussions, where metaprogramming is understood to mean… well, I’m not sure what it’s understood to mean, the way it’s getting used in connection with Ruby, and that’s the problem.There’s no natural separation between programming and metaprogramming in Ruby, and no reason to make life harder by breaking them apart. The thrust of Ruby’s design is to dissolve complexities, so that even things that appear to be “wizardly” or full of “dark magic” actually make perfect sense in terms of a relative small number of underlying language principles.
So here’s your takeaway #{max gagging sound}: method_missing is cool. It’s metaprogramming. It’s used to save programmer time and typing, and to make DSLs or provide default behavior. Most famously in ActiveRecord dynamic finders. Metaprogramming is not scary, and method_missing, like other cool Ruby features, may or may not float your boat. That’s cool, either way.
_why says: “I never use method_missing. Maybe twice. And both times I didn’t use it, regretted it, forcefully ejected the code from a moving vehicle, shed nary a tear.” Then he goes on to enumerate some of the very cool ways he’s seen method_missing used elsewhere, in non-_why code.
So: know it, love it, use it, hate it, kill it, eat it, break it, think it. As they say at Ari’s preschool: “Have fun, be safe, and USE YOUR IMAGINATION!” Matz wants you to.
Theoretically Related Posts
Trackbacks
Use this link to trackback from your own site.

Another good one to know in conjunction with method_missing is define_method. Basically, if you know at compile-time, err, read-time, what methods you want to dynamically define, you can define them and not have to pay the penalty for them being dynamic. I had some code where I added some methods to the String class to check whether it was valid for a given regular expression. I implemented it first with method_missing, then my coworker showed me the define_method way to do it. I don’t know how well this code will show up in here, but here goes:
valid_regexes = {
:host => Regexp.new(/^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}$/ix),
:macaddr => Regexp.new(/^([a-f0-9]{2}:){5}[a-f0-9]{2}$/) # Insist on lowercase
}
class String
# Define the valid_*? methods.
valid_regexes.each { |type,regex|
define_method(”valid_#{type}?”) { (self =~ regex) != nil }
}
end
“gnufoo.org”.valid_host? -> true
“blah”.valid_host? -> false
[…] method_missing is one of Ruby’s coolest bits of metaprogramming voodoo. Two years, Why wrote about it, and now Amy Newall, half of a Massachusetts-based husband-wife development team, writes about 10 things you should know about method_missing. […]
Just a note - I believe that some of the stuff that Rails used to implement with method_missing is now handled with code generation instead, due to method_missing being expensive in terms of time.
[…] 10 things you should know about method_missing […]
Point 3, First it looks at local variables, then at instance methods.
[…] performance problem with this is explained very well at thirdbIT’s post on method_missing. Every time you call method missing, you’re guaranteeing a full search through the […]
today i was working with this method_missing stuff, later i gave up, as because the end developer won’t get any hints about available methods.
this feature is obviously great, similar feature found over php __set __get.
i like active record except this part, though they have used “method_define”, but it seems odd, because i have to rapidly look on my table structure to know about the available fields on the domain object.
anyway, thanks for nice stuff!
[…] example, he emailed me the day that Ruby Inside flagged my method_missing article. Do I talk about that method_missing articleway too much, or what? I could talk about how I was the star quarterback on my high school football […]
[…] August I wrote a blog entry about method_missing which somehow got Reddited and then mentioned on Ruby Inside, which resulted in a huge spike in […]
[…] implements some of its funkiest magic with method_missing because ActiveRecord::Base overrides the Kernel’s method_missing […]
[…] 10 things you should know about method_missing เขียนง่ายดี และเป็นที่ที่ผมได้ลิงก์ของบทความ Jamis Buck ด้านบนครับ […]
[…] 10 things you should know about method_missing […]
[…] les langages dynamiques1 se permettent de pratiquer la magie noire (au moyen du concept de method_missing popularisé par Ruby), je vous propose de mettre en œuvre notre meilleur vaudou pour obtenir, en […]
[…] Write comment After some looking around here is what i found and thought others would find useful as well Exception speed in Ruby Exceptions in general are really slow in ruby and should be thought of as exceptional. method_missing is not an exception but is actually a kernel method of ruby. When you invoke a method on ruby the runtime will search the entire class and module heirarchhy all the way to object, class and module and then as a last case call method_missing. Its more like it just calls this method and you can override this method at anypoint in the chain. Its definitely slower than calling a method that does exist because it has to search up the heirarchy but it is definitely faster than exceptions. Here are some other posts that i found useful during my review of this area: http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work http://www.thirdbit.net/articles/2007/08/01/10-things-you-should-know-about-method_missing/ […]