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_missing
catches the call and sends the method to@obj
normal_method
raises an exception that is rescuednormal_method
does not equalpesky_method
so the error is re-raised…raise
is not a method so it is sent to method missingraise
is called on@obj
and an exception is raised, which is caughtraise
is not equal topesky_method
so 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
!