Today I was coding up a tricky bit of logic where I was using BasicObject to
delegate all methods to another object it was encapsulating and I learned that
raise is not a keyword in ruby.
To give a bit of detail here is generically what I had coded up…
|
Everything looks okay with this little patching class right? Little did I know I had created an infinte loop that was very difficult to debug. I’ll quickly walk through the stack and explain what happened.
- GateKeeper receives method
normal_method method_missingcatches the call and sends the method to@objnormal_methodraises an exception that is rescuednormal_methoddoes not equalpesky_methodso the error is re-raised…raiseis not a method so it is sent to method missingraiseis called on@objand an exception is raised, which is caughtraiseis not equal topesky_methodso the error is re-raised…- ….
Now you understand the infinite loop. This little bugger evaded me for awhile
and I learned a very powerful lesson, raise is just a method, not a keyword
in the ruby language. If you find yourself havinging the same problem you can
use Kernel.raise to throw an exception. Rule of thumb, when using
BasicObject don’t expect any methods to work, including raise !