Today we’ll explore a not so widely used feature in Ruby: overriding the the unary operators for fun and profit:
This must be one of the coolest, yet quite unknown, technique in Ruby. For certain types of problems (e.g. when you have a set of rules) this gives you such an elegant way of describing the solution. There’s no meta programming or monkey patching involved, it’s short and sweet and best of all: it’s very intuitive.
Let’s have a look at this and a few other tricks you can do by overriding the unary operators.
You can overload the unary operators???
You probably already know that it’s possible to override pretty much any operator in Ruby, so why should unary operators be treated differently? It makes very much sense when you think about it, but for some reason, most Rubyists never think about it. Unary operators just seems like a part of the language. Time for some myth busting, eh?
Woah, woah, woah. Hang on a second.
~@? What’s the matter with these weird method names? Let’s fire up IRB and see:
Like any other method, you can call them the usual way (
r.-@()), but it also turns out that Ruby uses these methods internally for the unary operators:
So how can we (ab)use these methods?
Example: Proxy with HTML rewriting support
Once upon a time there was a Ruby proxy which worked pretty much like Greasemonkey: You could easily modify any page before it hit the browser. By using the unary operators you could create scripts and define which pages it should rewrite:
How does it work? Simply take the Rule class above and combine it with this:
Now you just need to loop through the rules to figure out if an URL matches or not. Ta-da!
Terrible Example: Negaposi
Who needs Ruby syntax when you have plus and minus? Negaposi gives you everything you need!
It get’s even better!
In Ruby 1.9, it gets even better: We can redefine the
not operator too:
Horrible Example: MaybeNot (1.9 only)
“Sorry, mate. I can’t reproduce the error on my machine.”
I’d love to see what you can (ab)use this for. Please let me know if you find a suitable usage, and I’ll update this article.