On The Horizon : Ten Things I like about Ruby 1.9

by Gregory Brown

To answer a question on RubyTalk the other day, I had to reference Mauricio Fernandez's nicely compiled list of Changes in Ruby 1.9. While I was there I took another walk through the whole thing.

There are of course some features I *don't* like.


a = ->(b,c){ b + c }
a.call(1,2) # => 3


But there are quite a few that I do, and here I've listed ten I think will totally rock. I use Mauricio's examples, so all credit goes to him. Also, this article is from February, so if you find any features below that have changed, shout and I'll update.

15 Comments

Florian Gross
2007-06-25 13:52:40
Just a quick note: I backported the new enumerator stuff to 1.8. See http://flgr.0x42.net/code/future-enumerator.rb


Oh, and "a = a.each" does nothing... right?

Gregory
2007-06-25 13:58:07
@Florian


I gotta check out the backport, sweet.


I think you're right about "a = a.each", I think it just returns self.

Mr eel
2007-06-25 17:49:47
From that list #owner and #receiver really catch my attention. I've found a similar feature in Javascript (caller, callee) to be useful at times. And yes... EVIL.


The hash syntax gets a big Yay from me. Less verbose and visually more compact. I think more readable all round. Using it to cheat named parameters is awesome!

choonkeat
2007-06-25 19:07:58
ruby hash going json? neat..
Gregory
2007-06-25 19:10:47
@choonkeat,


It's also just an optional syntax, so you can still use the old way too. :)

Daniel Berger
2007-06-25 20:33:20
External iterators - ick.
Mandatory arguments after optional arguments - ick.
The "&:" proc passing syntax - ick.
The keyword argument syntax - ick.
chromatic
2007-06-25 23:30:02
p6meter++ ;)
JEG2
2007-06-26 06:47:16
Daniel:


Enumerator is not an external iterator. See the generator standard library for that.


Enumerator is used to switch which method is considered each(), so you can combine Enumerable iterators. For example, a different way to do map_with_index() in Ruby 1.9 than what Greg showed is:


...each_with_index.map { |e, i| ... }


Here I am switching the normal each() iterator out for each_with_index() and then calling map() normally.


James Edward Gray II

Daniel Berger
2007-06-26 18:37:01
JEG2,


Ok, but I still don't like it, and I think it will only make programs more difficult to read and debug, especially for the newbs. I think it's misguided.

Gregory
2007-06-26 18:38:43
@Daniel


That kind of surprises me. I feel like Enumerator is a godsend. :)

Daniel Berger
2007-06-27 09:21:55
Gregory,


I guess I just don't see what problem it solves that a custom definition and/or Object#extend doesn't already solve, and in a less obscure way.

Aredridel
2007-06-27 14:18:22
Sweet. Nice summary!
Gregory Brown
2007-06-27 16:35:43
@Aria,


Thanks!


@Daniel,


I'd love to see an ORA blog post of how you can elegantly live without Enumerator for the tasks it solves. If nothing else, it'd serve as a good way to explain what tasks it does solve! :)

EricL
2007-06-28 06:18:13
Is it just me or Ruby syntax is VERY confusing to read :(


I really like Rails concepts, and I want to learn Ruby. But it's syntax always discourages me. I'm afraid Ruby syntax readability will make maintenance of my applications in the future troublesome ...

Gregory
2007-06-28 08:02:34
@EricL,


That's actually somewhat surprising to hear. Most people find Ruby fairly readable. Let me say however that the code above is *not* a good place to start. Except for a handful of features there, most of that stuff is highly advanced functionality, and would be quite confusing if you didn't already have a strong grasp of the language.


What programming background are you coming from? Depending on the languages you already know, I can maybe point you at some helpful resources / books.