Search

Ruby Moment of Zen: #method_missing

Derek

2 min read

Jul 3, 2006

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.

    class User < ActiveRecord::Base   has_one :profile end  class Profile < ActiveRecord::Base   belongs_to :user end

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:

    def street_address  profile.street_address end

Here’s all you have to do:

    class User < 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.

Angie Terrell

Reviewer Big Nerd Ranch

Angie joined BNR in 2014 as a senior UX/UI designer. Just over a year later she became director of design and instruction leading a team of user experience and interface designers. All told, Angie has over 15 years of experience designing a wide array of user experiences

Speak with a Nerd

Schedule a call today! Our team of Nerds are ready to help

Let's Talk

Related Posts

We are ready to discuss your needs.

Not applicable? Click here to schedule a call.

Stay in Touch WITH Big Nerd Ranch News