Wednesday, December 03, 2008

I admit it: I love monkeypatching

One of the things I like most about writing in Ruby is that if I find myself reimplimenting the same thing more than two or three times, I have the option of extending Ruby's base classes to incorporate that functionality.

People argue against this (usually by calling it monkeypatching), but when it comes down to it, I think it's a wonderfully fun way to interact with an interpreted language, even if it's not the safest or prudent habit. After all, the more fun something is, the more dangerous it's got to be, right?

An example base extension I use a lot is binarize -- it takes a String or Array and turns it into binary (really, a pack("C*") string). Here it is at pastie. Once implemented, "414243".binarize magically turns into "ABC." Delight all around.

The argument against monkeypatching is that it's not that much more work to create a module with a function of binarize that takes an argument, so you end up with something like NoFunAtAll::binarize("414243"). But really, only squares with C.S. degrees would do that. Devil-may-care types like myself prefer to extend String and Array directly, and damn the maintainability!

Labels:

1 Comments:

Anonymous postmodern said...

Clever monkey patch. They make working with data easier.

I've also been building up a collection of monkey patches. Of interest are the formatting and URI::HTTP#query_params extensions.

10:00 PM  

Post a Comment

Links to this post:

Create a Link

<< Home