Ruby Moment of Zen: #method_missing

Realizing that hitting “talk” on my phone dialed the previous number. Figuring out that the arrow next to my fuel gauge showed which side the car fuel door is on. Tab completion.
A couple of times a month, I’ll have a “wow - that’s so useful and so simple” moment. One of the first times I experienced that with Ruby was with #method_missing.

Every class in Ruby has a #method_missing method. When you attempt to call a method that doesn’t exist, #method_missing will throw a NoMethodError exception.

You can override this method and do some amazing things with it. At High Groove Studios, we use #method_missing like a trophy wife uses her rich husband’s credit card bill: without abandon.

For example, let’s say you have a User model, and that user has a ton of profile data. You want to abstract that data, so you make a Profile class.

However, you don’t want to call user.profile.street_address every time you need to access an attribute in a user’s profile. You also don’t want to define a bunch of reader methods like this:

defstreet_addressprofile.street_addressend

Here’s all you have to do:

classUser<ActiveRecord::Base...# Attempts to pass any missing methods to # the associated +profile+. def method_missing(meth, *args, &block) if profile.respond_to?(meth) profile.send(meth) else super end end end

So, we now have this:

@user.street_address==@user.profile.street_address

That’s only one example of the power and simplicity of #method_missing.