April Fools': Making Ruby Undebuggable
Ruby is famous for letting you override any
method on any class, even classes that are part of the
core runtime. If you are evil and hate your colleagues, you could abuse
these properties to make Ruby objects randomly lie about whether or not they
are nil
.
The concept of a nil
value is pretty
central to Ruby. It is like checking for a null pointer in C, or asking
whether some Python object is None
. You can call
.nil?
on any object, and it will return True if it
is the (singleton) nil
object, or False if it is
literally anything else.
I have written up some example code in this
gist
which shows how to define a wrapper around the built-in
nil?
function which will return the wrong result at
random. You need to apply this module to both the
Object
and NilClass
classes,
in order to affect both nil
and non-nil
variables.
In this proof of concept, I made it lie with 33% odds. A much more devious approach would be to make it lie with 0.001% odds, or even lower. Ruby programs of any complexity check for nil values all the time. If you make the probability too high, it will be obvious that something is wrong because the application will be extremely unstable. But if you make it just slightly higher than zero, the application will misbehave (in random, unrelated parts of the codebase) just often enough to drive your collaborators completely insane. None of the bugs they encounter will be reproducible.
This is something that only a real bastard would do. Hopefully your team does rigorous code review.