What Is New in Ruby 1.9
Ruby 1.9 was a significant evolution of the language, introducing a new virtual machine, a major syntax update, and a host of new core features. This release laid the groundwork for the modern Ruby we use today.
| Category | Key Changes |
|---|---|
| New Features | New VM (YARV), New Hash Syntax, New Literals, Block Parameters |
| Syntax Changes | New Block Scoping, Optional Hash Braces, Character Literals |
| Standard Library | New RubyGems, New JSON Library, Psych (YAML) |
| Encoding | M17n (Multilingualization) Support, Source Encoding |
| Deprecated / Removed | ParseDate module, env value for safe_level |
How did the VM and performance change in 1.9?
Ruby 1.9 replaced the old MRI interpreter with YARV (Yet Another Ruby VM). This was the single biggest change for performance. YARV uses a bytecode-based compilation model, which drastically speeds up execution compared to the AST-walking interpreter in 1.8.
In practice, you could expect most programs to run 2-3 times faster. This was a game-changer for Ruby's viability in larger applications and helped pave the way for frameworks like Rails to handle more scale.
What new syntax did Ruby 1.9 introduce?
The new hash literal syntax was the most visible change. It allowed using a colon (:) instead of the hash rocket (=>) for symbol keys, making hashes much cleaner to write.
# Old 1.8 style
old_hash = { :key => :value }
# New 1.9 style
new_hash = { key: :value }
Block parameters gained a powerful new syntax for declaring local variables. This made blocks more self-contained and predictable by preventing variable leaks into the outer scope.
How did encoding and internationalization work?
Ruby 1.9 introduced M17n (Multilingualization), a completely new system for handling string encodings. Every string now has an associated encoding, making Ruby much better at handling non-ASCII text.
You could specify a source file's encoding with a magic comment at the top. This was crucial for using Unicode literals in your code.
# encoding: utf-8
string = "ã"ã‚"ã«ã¡ã¯"
What was new in the standard library?
RubyGems was finally bundled with the interpreter, which meant you no longer had to install it separately. This made dependency management a standard part of the Ruby experience out of the box.
The library saw major additions like the json gem and the psych YAML parser, which replaced the older syck. The prime library was also added for working with prime numbers.
FAQ
Is Ruby 1.9 backward compatible with 1.8?
Mostly, but not entirely. While many programs will run unchanged, there are breaking syntax changes (like new block scoping) and removed features. You should test your application thoroughly before upgrading.
What is the most important change for a developer to know?
The new hash syntax and the block-local variables. The hash syntax is everywhere and you'll see it immediately. The block scoping change can fix subtle bugs but might break existing code that relied on the old behavior.
How do I specify the encoding of a source file?
Use a magic comment on the first line of the file. The most common one is # encoding: utf-8. If the first line is a shebang (#!/usr/bin/env ruby), the magic comment goes on the second line.
Was the standard library structure changed?
Yes. Many standard libraries that were previously pre-installed, like soap, xsd, and xmlrpc, were moved to external gems. You now need to explicitly add them to your Gemfile.
What happened to the ParseDate module?
It was deprecated and eventually removed. The recommended way to parse dates is to use the Date.parse method from the date standard library instead.