Be Prepared for Ruby 1.9.3 and 1.9.4: What’s New and What’s Different
On August 1, 2011, Ruby 1.9.3 preview 1 was released. The final version isn't yet out (as of September 23) but Ruby 1.9.3 is going to be the next, full production-level release of MRI Ruby. But what's the deal with 1.9.3 (and its successors, Ruby 1.9.4 and 2.0)? Keep reading!
Ruby 1.9.3 is a relatively minor improvement on the Ruby 1.9.2 we already know and love. In short:
- a focus has been placed on performance with file loading, File and Pathname all significantly improved
- Ruby 1.9.2 mostly fixed down the language specification for 1.9; 1.9.3 is mostly work on a 'better implementation'
- you can tune the garbage collector via environment variables (more on this in a post coming soon!)
- Ruby's license changes from dual Ruby + GPLv2 to dual Ruby + 2-clause BSD
- improved GC performance with a lazy garbage collector
- a 'better strategy' for the GIL / GVL
- test/unit supports parallel testing
- Random.rand tweaks (and rand() now accepts ranges)
- io/console, a new library in the stdlib
- 4 new encodings (so far) - cp950, cp951, UTF-16, and UTF-32
- extra String methods
- a number of tweaks to formatting strings
- Module#private_constant and Module#public_constant
- a smattering of other bits and pieces, but this is the TLDR overview!
For some examples of the above, however, read on.
Yuki 'yugui' Sonoda on Ruby 1.9.3
At RubyConf Taiwan (held August 26-27, 2011), core team member Yuki 'yugui' Sonoda gave a talk called Ruby 1.9.3 and Ruby 1.9 Development which outlined her current thinking on Ruby 1.9.3, Ruby 1.9.4, and Ruby 2.0. It's not very long and worth a watch.
Yugui's talk was only short but the key points were that:
- Ruby 1.9.2 essentially fixed down the language design for 1.9; 1.9.3 is just a 'better implementation'
- Ruby 1.8 has "no future" (she stated this a few times) but it will be "supported" for a few years to come
- there is no intention to release a Ruby 1.8.8
- "You need to switch to Ruby 1.9"
- Ruby 1.9.3 will be out very soon
- the license was changed to joint BSD because the release of GPLv3 forced a rethink on licensing
- the locking strategy related to the GIL / GVL has been improved, resulting in improved performance
- test/unit's parallelization features are well suited for testing Ruby's stdlibs more quickly
- you can "safely switch to Ruby 1.9.3 from Ruby 1.9.2" as there are "few incompatibilities"
- the new "lazy GC" will improve the response time of the garbage collector and decrease overall GC throughput - more info in this article, for the curious
- Yugui is not particularly familiar with RubySpec - it seems to continue to not be a focus for core MRI implementers
A Selection of 1.9.3 Changes
I've cherry picked a few changes in 1.9.3 to highlight.
Faster loading - the load.c patch
Recently, I wrote all about the load.c file loading performance saga in Ruby 1.9.2. These issues have begun to be addressed and a nifty patch has enabled Ruby 1.9.3 to post significantly improved loading times for apps with large trees of files to load. You could see anywhere from a 5% to 40% reduction in load times for your apps.
Time#strftime supports %:z and %::z
Time.now.strftime("%:z %::z") # => "+01:00 +01:00:00"
Time#strftime now supports some extended formats for timezones.
:z includes the minutes and
::z gives the full HH:MM::SS.
String#prepend and String#byteslice
Among some changes to the String class are a couple of new methods. First,
a = 'world'; a.prepend('hello '); a # => "hello world"
String#prepend prepends one string to another, in-place. The result is equivalent to using
a[0,0], in this case.
byteslice will let you get access to sections of a string at the byte (rather than character) level:
a = 'hello'; a.byteslice(2, 2) # => "ll"
New character encodings
CP950, CP951, UTF-16, and UTF-32 encodings have been added. Previously, UTF-16 and UTF-32 were available in big-endian (BE) and little-endian specific forms. For example:
Encoding.find("UTF-16LE"). I have not yet confirmed if the new UTF-16 and UTF-32 encodings support the byte ordering marks that they should, but am currently assuming so. CP950 and CP951 are Microsoft encodings for Chinese character sets.
Changes to 'Random'
The Random class now accepts ranges on its
rand class method. In 1.9.2, this was only allowed on its instance method. For example:
Random.rand(5..9) # => [a number between 5 and 9]
A side effect of this is that
Kernel.rand now also supports ranges in 1.9.3. For example:
rand(5..9) # => [a number between 5 and 9]
#__id__ moved from Object to BasicObject
BasicObject is the new grand-daddy of the inheritance tree in Ruby 1.9 and in 1.9.3 it gains access to the functionality of the
object_id method by way of
__id__ which has been moved into BasicObject from Object. This may help those of you using BasicObject for delegators, proxies, and the like.
BasicObject.new.__id__ # => 2152881920
If you want to pick up on more changes, see the Ruby 1.9.3 NEWS document or, perhaps, check out my Ruby 1.9 Walkthrough screencast ($16) which has an entire section dedicated to things that are new in Ruby 1.9.3.
The Future: Ruby 1.9.4
In her talk, Yugui mentioned two extra production releases of Ruby to come after Ruby 1.9.3. The first was unnamed and was said to be a 1.9 release with minor language changes to 1.9.2/3. In the Q&A, someone asked Yugui if this would be called Ruby 1.9.4 but she said it was still under discussion but hoped it would be called 1.9.4. This unnamed next release would, however, have complete backwards compatibility for code written in Ruby 1.9.2/3.
The Future: Ruby 2.0!
The second release after 1.9.3 "should be called Ruby 2.0", however. It will have significant changes but Yugui notes that "it should be comfortable with 1.9.3" and that you should be able to "safely switch" to 2.0, indicating that any significant changes wouldn't involve removing core language elements or permanently changing core syntax features.
It has previously been said, however, that Ruby 2.0 could be "several years" away, so don't get too excited about this yet. Now is the time to start weighing if you want to influence Ruby 2.0's design, however!
Still getting to grips with Ruby 1.9? Try my walkthrough
I recently released a screencast called Ruby 1.9 Walkthrough, a mega walkthrough of what's new in Ruby 1.9.2 (and Ruby 1.9.3) from the perspective of Ruby 1.8.7. If you're still primarily a Ruby 1.8 based developer and aren't entirely sure about what's new in Ruby 1.9, check it out.