This Week on Github: Stupid Ruby Tricks

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. Here are some examples.

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:

class String
  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:

irb(main):014:0> "Jeremy" <+ "McAnally"
=> "yllanAcMymereJ"

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:

my_object = Swizzler.new
my_object.class = ThisOtherClass

Changing the superclass is nearly just as easy:

my_object.superclass = ThatClass

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;

class InheritedClass
  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.

Comments on "This Week on Github: Stupid Ruby Tricks"


| 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:

I believe that there is a small typo in the text quoted above:

\”if you wanted to define a superator +< to conjoin\” should be
\”if you wanted to define a superator <+ to conjoin\”

Does your site have a contact page? I’m having a tough time locating it but, I’d like to send you an email. I’ve got some creative ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time.

Howdy just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Safari. I’m not sure if this is a format issue or something to do with web browser compatibility but I thought I’d post to let you know. The design and style look great though! Hope you get the issue fixed soon. Cheers

Hey there. I discovered your site by the use of Google even as looking for a related matter, your web site came up. It appears great. I have bookmarked it in my google bookmarks to come back then.

I love the efforts you have put in this, appreciate it for all the great posts.

I do agree with all the ideas you have presented in your post. They’re very convincing and will certainly work. Still, the posts are too short for beginners. Could you please extend them a little from next time? Thanks for the post.

I gotta bookmark this website it seems invaluable very beneficial

I am curious to find out what blog platform you’re using? I’m having some minor security problems with my latest blog and I’d like to find something more safe. Do you have any solutions?

you will have a great blog here! would you wish to make some invite posts on my blog?

You are my intake, I own few blogs and occasionally run out from to brand : (.

“Hello, you used to write wonderful, but the last few posts have been kinda boring???‚¬?¦ I miss your tremendous writings. Past few posts are just a bit out of track! come on!”

“I have really noticed that repairing credit activity must be conducted with tactics. If not, you are going to find yourself damaging your positioning. In order to be successful in fixing your credit score you have to take care that from this instant you pay all of your monthly dues promptly prior to their booked date. It is significant simply because by definitely not accomplishing this, all other activities that you will decide on to improve your credit ranking will not be effective. Thanks for discussing your concepts.”

I as well think so , perfectly written post! .

This post have resolved my problem,thanks very much and hope you writting more good articles.
oakley big taco sunglasses matte black fire iridium outlet http://www.fleetsale.ru/oakley-big-taco-sunglasses-004.html

Some genuinely excellent blog posts on this site, thanks for contribution.

I simply want to mention I’m new to blogging and site-building and really enjoyed you’re blog site. Likely I’m likely to bookmark your website . You surely come with fabulous well written articles. Thanks a lot for sharing with us your web site.

Nice post. I learn something totally new and challenging on sites I stumbleupon on a daily basis.
It will always be helpful to read through articles from
other authors and practice something from other web sites.

My site … LeifVCondren

wYhFOc whumnbeusmag, [url=http://vywxhuydomsb.com/]vywxhuydomsb[/url], [link=http://xxqaqjxvzttw.com/]xxqaqjxvzttw[/link], http://fpbjsozeopdp.com/

Always a significant fan of linking to bloggers that I really like but don?t get lots of link enjoy from.

cartierbraceletlove ??? ?????? ????????? ??? ?????? ????? ?? ?????? – ????????? ??????, ??????? ??????????. ?????? ? ?????? ?????????? ??????????? ?? ???? ????????? ??????? ?? ?????. ????? ?? ?????? ????????? ?? ?????????!
faux van cleef and arpels bague

Always a big fan of linking to bloggers that I love but do not get quite a bit of link enjoy from.

Leave a Reply