litany against fear

coding with spice ¤ by nick quaranto

RailsConf 2009 Getting to know Ruby 1.9

published 06 May 2009

This is part of my series of notes on RailsConf 2009. Check them all out here.

This talk is from David A. Black. No slides for this presentation: all examples of some live code. Going to go over what’s needed to know to upgrade existing 1.8 code and then dive into what’s new.

In 1.8: Object#to_a works pretty well, but warns you that it will be obsoleted. In 1.9 it’s gone. One way to get around this: use Array() to create arrays. There’s also Integer() and Float() which are a little stricter than to_s and to_f.

No more comma delineated hashes. a = {1,2,3,4,5,6} just doesn’t work.

In 1.9, String does not have Enumerable mixed in. This caused a lot of confusion with 1.8 since String#each and String#map behave differently with line breaks. There’s now a few methods: String#each_line, String#each_char, String#each_byte, and also String#each_codepoint. Codepoints are used for multibyte/UTF8 strings.

Finally, String#[] returns a character and not a number.

New Hash shortcut with symbols: h = { one: 1, two: 2, three: 3 } They’re also ordered too. When walking through the Hash, it’s done by the order that key/value pairs were inserted into it.

Plenty of new Enumerable magic: #each_slice splits arrays into parts, #each_cons does a sliding door of the given size. It’s now possible to use Enumerable#cycle to get a sort of manual iterator that you can stop and start.

Block scoping with variables works the way people expect. If a variable inside a block has the same name as a local variable, it will use the block scoped variable.



code (2) gaming (3) git (9) internet (5) prorubyconf (25) railsconf (17) ruby (10) windows (3)