Method_missing hazardous to your module?

February 22, 2013

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 some dynamic refactoring!

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


  1) MyHelper#filter returns an empty filter if there's nothing in the session
     Failure/Error: not_my_method
       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.

method_missing hazardous to your module? - February 22, 2013 - Ken Mayer