featureenvy

The blog of a thinkerer.
By @featureenvy

All rights reserved.

Ruby 2.0 Reaches Feature Freeze: What Will Change?

Yes, you read right: Ruby 2.0 is in feature freeze! This doesn't mean it will be released in the next month. But it introduces us the most important changes that will come with version 2.0. The timing is actually pretty impressive, since this feature freeze was announcement by Yusuke Endoh one year ago! Ruby seems to be on track (pun totally intended)! Want more details? Read on!

The Release Plan

Right now, there are two important milestones left: Code freeze and release. Code freeze is planned at the end of the year. So all the bugs should be more or less ironed out by then. And then, on February 02, 2013, 20 years after Ruby became part of this world, Ruby 2.0 should be released!

The New Features

There isn't yet an official list out with all the new features, but the source code, commit history and discussions are open to the public, so it is no secret! This isn't a big release as the change from 1.9 to 2.0 would suggest, there are only a handful of new features:

Refinements

Refinements are one of the more interesting changes that could actually change the way Ruby code is written. It allows you to monkey patch a class, but only where you want to, and not in the global scope. Monkey patching could actually become an accepted thing to do! Some code? Sure:
module StringExt
  refine String
    def length
      42
    end
  end
end
Here we define a module that contains a refinement (refine) for String. Because length should always be 42 (for whatever reason...). Let's define a class that could use it:
class TheAnswerToLifeTheUniverseAndEverything
  def self.answer(the_question)
    the_question.length
  end
end
But that is wrong! And that's right. No, wait, I mean, the answer is wrong, but the rest is right. That's because we didn't use the refinement yet, we just defined it. Let's use it then:
class TheAnswerToLifeTheUniverseAndEverything
  using StringExt

  def self.answer(the_question)
    the_question.length
  end
end
If we ask for TheAnswerToLifeTheUniverseAndEverything.answer("I want the answer to everything!!! NOOOOOW") it will tell us 42. Exactly what we want. But the behavior of String#length outside of our class hasn't changed. At all! Only when we use it inside class that is using this refinement is the behavior changed. Which could be really handy!

Named Arguments

Another one that we might end seeing a lot is named arguments. Named arguments could replace the options hash that is so prevalent in todays Ruby code. Use def namedParameters?(yes: true, no: false) [...] end. Then there are some internal changes and some speed improvements for MRI. Nothing exciting, but definitely good changes.

Will It Break Stuff?

Most likely? No. The most likely candidate to break stuff? respond_to?. The behavior of respond_to? in regards to protected methods has changed. By default, in 2.0, it doesn't search for private or protected methods, in Ruby 1.9 it only excludes private methods. The amazing Tenderlove has a great writeup about this. Apart from that, it seems like there is nothing else that will break with Ruby 2.0, so the upgrade should be easier then going from 1.8 to 1.9. Even though the roadmap is actually pretty big!

Yay! Exciting Stuff!

I'm looking forward to Ruby 2.0. I'm especially interested in what kind of clever hacks people will come up with now that we have refinements. What are you looking forward to? What is still missing in your opinion? If you liked this post, you should consider hiring me freelance, part-time or full-time!