What Is New in Ruby 2.2
Ruby 2.2 brings significant garbage collection improvements, new methods, and incremental enhancements across the standard library. This release focuses on optimizing memory usage and providing more powerful tools for developers.
| Category | Key Changes |
|---|---|
| Garbage Collection | Incremental GC, Symbol GC |
| New Methods | Binding#receiver, Module#prepend, String#unicode_normalize |
| Libraries | Psych, RDoc, RubyGems updates |
| Performance | Fork speed improvement, malloc/calloc optimizations |
| Deprecations | DL, ext/tk, pre-1.9 proc arg handling |
How did garbage collection get smarter in Ruby 2.2?
The headline feature is the new Incremental Garbage Collector. Instead of stopping your entire application for a full GC cycle, the work is broken into smaller pieces and interleaved with program execution. This drastically reduces pause times, making applications feel more responsive.
Ruby 2.2 also introduced Symbol GC. Previously, symbols were never garbage collected, which was a potential memory leak vector for long-running apps that created symbols dynamically. Now, symbols created from strings can be collected, which is a big win for security and memory usage.
What new methods should I start using?
Binding#receiver is a handy addition that returns the object that owns the binding's context. This is useful for metaprogramming and debugging, giving you direct access to the receiver object.
Module#prepend is now officially supported after being experimental. It allows you to insert a module in front of a class in the ancestor chain, which is more powerful than include for certain kinds of method wrapping and refinement.
The new String#unicode_normalize method and its bang variant provide built-in Unicode normalization, making it easier to handle different character representations without external gems.
Which libraries received major updates?
Psych
The YAML parser Psych was updated to libyaml 0.1.6. This update includes various bug fixes and improves the overall stability and correctness of YAML parsing in Ruby.
RDoc
RDoc got a significant update to version 4.2.0. It includes numerous improvements for generating documentation, with better parsing and formatting capabilities.
RubyGems
RubyGems was updated to version 2.4.5, which includes various bug fixes and performance improvements for managing gem dependencies and installations.
Were there any performance wins beyond GC?
Yes, the process of forking became much faster on systems with copy-on-write friendly malloc implementations. This is a major benefit for web servers like Unicorn that rely heavily forking worker processes.
Ruby's internal memory allocation was optimized to use malloc/calloc more efficiently, reducing memory overhead. These low-level optimizations contribute to overall better memory footprint and performance.
What was deprecated or removed?
The DL standard library for interfacing with C libraries was deprecated in favor of the newer Fiddle library, which provides a more maintainable and cleaner API for the same functionality.
Support for the Tk graphical user interface toolkit (ext/tk) was moved to a separate gem, reducing the size of the core Ruby distribution for users who don't need GUI capabilities.
The pre-Ruby 1.9 style of Proc invocation where a single array argument would be automatically expanded was finally removed, solidifying the modern behavior.
FAQ
Does the incremental GC require any code changes?
No, it's completely transparent. Your existing code automatically benefits from reduced garbage collection pause times without any modifications.
Why is Symbol GC such a big deal?
Before 2.2, dynamically created symbols (like from params in a web app) would never be freed, creating a memory leak attack vector. Now they can be collected, making apps more secure.
When should I use Module#prepend over include?
Use prepend when you need to override methods in a way that still allows calling the original method with super. It inserts the module before the class in the ancestor chain.
What's the practical benefit of faster forking?
Web servers that use forking models (like Unicorn) can restart workers much quicker, leading to better deployment speed and responsiveness during traffic spikes.
Is it safe to upgrade from Ruby 2.1 to 2.2?
Generally yes, as it maintains backward compatibility. The main things to check are the deprecated DL library if you use it, and ensuring any C extensions are compatible with the new GC changes.