Rubyisms: reopening classes

Written on 2:26:00 PM by S. Potter

Ever wished you could write code in a very expressive way like the following examples?

  • expiration_date = 3.years.from_now
  • birth_date = 32.years.ago
Now you can, in Ruby at least. Two years ago I was a huge Python head that thought no other language could compete with Python for developer productivity. My Python indoctrination occurred after 7 solid years of proclaiming Java was the ultimate language that would reign supreme. Prior to my Java phase I was a C++ and Perl coder. It was also two years ago that I got my first glimpse at Ruby. I read articles, blogs and forums between Python and Ruby heads discussing finer points of each language and how one is far more superior than the other, because....bla bla bla. It was at this time that Ruby's concept of reopening classes came to my attention. As a hearty Python developer at the time I came to the conclusion that Python had to be superior on this point because Python allowed class reopening through metaclasses, so the original author of the code could be explicit to allow class reopening or not. In Ruby, however, anyone could reopen your classes, write malicious code and redploy without others knowing. Over the past two years I have realized that while Python is a beautiful programming language (not just scripting language) in its own right, my views on this point have changed considerably. To show class reopening I will be using an example of code that is very similar to some Ruby standard library extensions found in Active Support included in Rails (Disclaimer: This is not necessarily the same underlying code - I have not checked to be honest). Let us start up an irb shell session and write the following:
  irb> 3.class
  => Fixnum
  irb> class Fixnum
  irb>   def years
  irb>     return self*365*24*60*60
  irb>   end
  irb> end
  => nil
  irb> 3.years
  => 94608000
  irb> 3.years.class
  => Fixnum
  irb> class Fixnum
  irb>   def ago
  irb>     return - self
  irb>   end
  irb>   def from_now
  irb>     return + self
  irb>   end
  irb> end
  => nil
  irb> 3.years.from_now
  => Sun Aug 09 14:23:10 CDT 2009
  irb> 3.years.ago
  => Mon Aug 11 14:23:19 CDT 2003
Did you follow what I just did? I wanted to show what 3's class is in Ruby. To those not very familiar with Ruby, this was important to show, since newbies might expect 3.class to be Integer, but in fact it is of type Fixnum. Next we reopened the Fixnum class for new definition. If we had reopened it and inserted a method called floor or ceil we would have overwritten the original Ruby standard library implementation of these methods. Instead we defined a method called years, which wasn't previously defined in Fixnum. Then we closed the class and found that 3.years returns 94608000, which is the number of seconds in a year. Is that useful? Not yet, so next we reopened Fixnum again and insert two methods: ago and from_now. After reclosing the class again, we can write partial sentences in Ruby and provide an extremely expressive API for an application that deals a lot with dates and converting numbers to dates. This couldn't be done in Python with the same ease using metaclasses, because the builtin int type cannot be reopened unless you redefined the int type from scratch in Python including the addition of your special purpose methods in the mix. Anyway, this might not convince all readers of the significant utility of the class reopening feature of Ruby, but I have began to love this feature, arguably too much (some may say). I have found it has significantly improved readability and condensed the code necessary to do many tasks. After converting to Java a number of years ago from Perl and C++, I vowed not to take the Perlist's attitude toward trusting clients of your code not to bastardize it. However, I find myself accepting that if a client of my code knowingly decides to take this risk, it is on their own head they fall. I should not be bothered by this professionally or personally. I should also bring to reader's attention that this feature of Ruby is a stepping stone to promote the development of true domain specific languages (DSL), which is difficult at best to do in many other languages.

If you enjoyed this post Subscribe to our feed

1 Comment

  1. Anonymous |

    Objective-C, which some call an ancestor of Java, also had this concept. It was called a "category". As a developer, you could extend any class with additional methods, or even override base methods in a category. This was very handy for things like asking a DoubleTextField for its valueIn32nds, even though DoubleTextField was a class from a third party library.

    There is an inherent danger to having this ability however, because in Objective-C (and as you say above) base methods could be overridden too. Since Ruby is an interpreted language it isn't quite as dangerous, but in Objective-C, it was possible to be unclear about which version of the method you were calling at runtime.

    At BoA, we had a convension in Objective-C that we never used categories to override base behavior, that is a task best left to subclasses. Of course, not everyone followed this, so debugging became more art than science. :)


Post a Comment