method_missing hazardous to your module?

February 23, 2013 Ken Mayer

We built an(other) object factory module for our current project and it looks a lot like all the others:

After a while, we noticed that the create methods were all exactly the same. Time for dynamic refactoring!

Unfortunately, this leads to rather bizarre error messages during testing:

Failures:

  1) MyHelper#filter returns an empty filter if there's nothing in the session
     Failure/Error: not_my_method
     NameError:
       undefined local variable or method `not_my_method' for ...
     # ./spec/support/object_creation_methods.rb:141:in `method_missing'
     # ./spec/helpers/my_helper_spec.rb:14:in

We became our own enemy; Since we are including our module in RSpec for our tests, any missing method passed through our dynamic response method, with unfortunate, confusing results. Sad Panda.

There’s a refactoring pattern, “Replace Dynamic Method Receptor with Dynamic Method Definition” which translates run time #method_missing into methods defined at load time. No more crazy stack traces!

Thank you! Jay Fields, et al.

About the Author

Biography

Previous
How to Use Mobile to Drive Conversions
How to Use Mobile to Drive Conversions

According to Google’s thinkmobile presentation, almost four out of five U.S. shoppers use their smartphone ...

Next
Building identical gems
Building identical gems

We ran into a problem where we were running `gem build` on identical input files and the built gems had dif...