Ruby is a very dynamic language. You can turn the language inside out, give objects a frontal lobotomy, and stitch the whole thing back together again, all at runtime.
Given its dynamism, many people have found abnormal even scary techniques to manipulate the behavior of Ruby. This week, let’s catalog just some of those libraries.
Sit back and prepare to be amazed.
Jay Phillips, author of the fantastic Adhearsion VoIP Ruby library, created a great hack called superators (http://github.com/jicksta/superators) to add operators to Ruby. Instead of the few operators the Ruby parser normally supports, like / or +, you can use any operator (or combination thereof) to manipulate values.
For example, if you wanted to define a superator +< to conjoin one string to another and reverse the resulting composite string, you could do something like this:
superator "<+" do |operand|
(self + operand).reverse
This code re-opens the String class and defines a new operator, <+. Inside the block body, the current object is self (as always) and the RHS of the operator is given to the block as an argument (in this case, operand). With this enhancement defined, you can do this:
Such a wild hack probably shouldn't ever be used in production code (with the exception of some sort of exotic, domain-specific language or something), but nonetheless it's really interesting. Check out the code at the repository or simply execute gem install superators to get started.
Heresy! 'Tis unholy!
The fine gentleman known as why the lucky stiff is a bit of a fixture in the Ruby community. All his code (located at http://github.com/why) is something of a testament to good and proper Ruby style, but a little library named unholy (http://github.com/why/unholy) is by far the, ahem, most blasphemous hack he's released. unholy lets you compile Ruby code into Python bytecode (then back into actual Python code using decompyle).
Once you install the code, simply run…
$ unholy my_ruby.rb
… to compile my_ruby.rb into my_ruby.rb.pyc. You can then run the code with Python (python my_ruby.rb.pyc). unholy doesn't support every scrap of Ruby syntax, but it is a really interesting experiment. If anything, it can end the argument that Ruby is too slow. Just convert it to Python bytecode. Problem solved!
It's Pure evil.rb
I saved the most evil for last. Aaron Bedra, denizen of the Ruby underworld, has published a library simply known as evil (http://github.com/abedra/evil). Along with some C code, there is a tiny little file called evil.rb originally written by Mauricio Julio FernÃ¡ndez Pradier and Florian Gross. The methods in this library give you the power to manipulate objects any way you'd like.
For example, you can unfreeze a frozen object (effectively removing any sort of object access control from Ruby objects), share or duplicate state (for instance, instance variables) between two objects, randomly allow you to bind a method to an object (with no concern for class compatibility), and many more evil things. Even outright changing the class of an object is as simple as:
The one interesting and possibly useful thing that evil offers is a workaround for Ruby's strict single inheritance through an inherit method. For example, if want to inherit from MyClass, YourClass, and YourMomsClass, that would normally be impossible. But not with evil.
class MyClass; end;
class YourClass; end;
class YourMomsClass; end;
inherit MyClass, YourClass, YourMomsClass
InheritedClass now has all the methods and state objects from the three classes it inherits from. Of course, in "normal" Ruby, you'd typically just have three modules and include those, but nonetheless it's an interesting hack.
And a Few Notable Mentions
I found a few other small libraries you might be interested in if you're looking for other ways to abuse Ruby...
Unfactor your code for your own job security! Ryan Davis is known for making Ruby code cry with his heckle and other related tools, but his transmogrify library takes the cake for utter perversion of Ruby code. It's unfinished, but you can use it in its current state to rewrite the names of variables to rot-13 or pig latin equivalent (or even rename them to douche(x) where x is an incrementing number).
Also be sure to check out Jay Phillip's other library, methodphitamine (http://github.com/jicksta/methodphitamine). It's an interesting hack to make working with collections in Ruby a little easier on the eyes, even if the hack is heinous.
Ben Bleything has a collection of stupid Ruby tricks (http://github.com/bleything). It's a small collection of things, but it offers a few fun things like switching all your getter and setter method logic and removing a random method.
If you've found any other stupid Ruby (or Python or PHP or C++ or...) tricks, then let me know on Github at jeremymcanally or @jm on Twitter.
Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62