More features that shipped with Ruby 2.0

David PadillaMar 05, 2013

I had the honor to attend Heroku Waza last week. The conference in general was amazing, however, there was a talk that (to me) was more interesting than any other.

Ruby 2.0 was released last week, and at Waza, Matz tried to give us a glimpse of all the new features that shipped with it.

image alt

Unfortunately, time was not enough and Matz only had a chance to talk in detail about the features that Akira Matsuda already described at RubyConf last year.

However, Matz did mention a few more, so, I decided to investigate a and write a little bit more about it, so, here they are.

Keyword arguments

If you come from a language like C#, this is very familiar, and its probably one of the things that you miss the most.

To do something similar in 1.9.2, you have to pass an options hash to the function where you want named parameters.

It works, however, things get ugly when you need defaults for those parameters.

It still works, but code begins to look complicated. You are doing things like merging a hash into another hash just to get the parameters of the options.

Enter Named Arguments.

As you can see, you get the same functionality with less code.

Also, you can combine them with regular parameters.

If you ask me, methods and their options are more descriptive this way. It will make Ruby code even more legible than it already is.

I can see Rails taking advantage of this all over the place.

To_h

We can now convert objects to hashes using the, very simple, .to_h that comes with some classes.

A simple example, I couldn't think of a better one right now. However, I am sure that we will see plenty of this feature being used in the wild very soon.

Symbol array literals

This one blew my mind, I loved it.

The example speaks for itself, get a list of symbols from a list of words. Beautiful.

DTrace support

I have never actually used DTrace, but, I've heard of it.

From it's Wikipedia page:

DTrace can be used to get a global overview of a running system, such as the amount of memory, CPU time, filesystem and network resources used by the active processes. It can also provide much more fine-grained information, such as a log of the arguments with which a specific function is being called, or a list of the processes accessing a specific file.

That sounds like a lot of debugging power. DTrace will become a powerful ally in the quest for memory leaks and any number of weird runtime errors.

Conclusion

Go upgrade your code to use Ruby 2.0 right now. It's not 100% backwards compatible with 1.9.x, but, it's pretty close.

So far I haven't found anything challenging when migrating apps from 1.9.

Also, I didn't mention performance. Ruby 2.0 + Rails 4 is faster. Noticeable faster. But, I'll write more about that in another post.

Cheers.

blog comments powered byDisqus