What Is New in Ruby 2.0
Ruby 2.0 introduced significant language refinements and performance enhancements. This release focused on making Ruby more expressive and efficient for developers.
| Category | Key Changes |
|---|---|
| New Features | Keyword Arguments, Module#prepend, Refinements, %i for symbol arrays, default UTF-8 encoding |
| Performance | Faster require, lazy symbol garbage collection, optimized VM and GC |
| Deprecations | Ruby 1.8.7 support, $SAFE=4, some legacy features |
| Libraries | New and updated standard libraries |
How did keyword arguments improve method definitions?
Keyword arguments are a major syntax upgrade in Ruby 2.0. They allow defining methods with named parameters, making method calls clearer and more self-documenting.
You can define required and optional keyword arguments, which eliminates the need for parsing a final options hash. This makes APIs much more intuitive to use.
Example
def establish_connection(host:, port: 3306, ssl: false)
# Method body
end
# Clear, explicit call
establish_connection(host: "db.example.com", ssl: true)
What is the purpose of Module#prepend?
Module#prepend provides a new way to mix modules into a class that takes precedence over the class's own methods. It inserts the module before the class in the ancestor chain.
This is powerful for overriding methods while still having access to the original implementation via super. It's a cleaner alternative to alias_method_chain patterns used in earlier versions.
Example
module Logging
def execute
puts "Starting..."
super
end
end
class Worker
prepend Logging
def execute
# Actual work
end
end
How do refinements change monkey-patching?
Refinements offer a scoped and controlled alternative to global monkey-patching. They allow you to modify classes within a specific module or file scope without affecting the entire application.
This addresses a major pain point in large codebases where conflicting monkey patches could cause subtle bugs. You activate refinements with the using keyword within a module or at the top of a file.
Example
module StringRefinements
refine String do
def shout
upcase + "!"
end
end
end
# Only active within this module
module MyModule
using StringRefinements
"hello".shout # => "HELLO!"
end
What performance enhancements were included?
Ruby 2.0 brought several under-the-hood performance improvements. The virtual machine and garbage collector were optimized for better execution speed and memory usage.
Lazy symbol garbage collection helps with memory consumption in long-running processes. The require method also became faster, reducing application boot time.
What syntax shortcuts were added?
New literal syntaxes made code more concise. The %i notation creates arrays of symbols without needing multiple quotes and colons.
Default source encoding was changed to UTF-8, eliminating the need for magic comments in most files. This reflects Ruby's modern, international use cases.
Example
# Old way
symbols = [:foo, :bar, :baz]
# New in Ruby 2.0
symbols = %i(foo bar baz)
FAQ
Are keyword arguments backward compatible with old option hash patterns?
Yes, Ruby maintains backward compatibility. Methods defined with traditional option hashes can still be called with keyword arguments, and vice versa in most cases.
When should I use prepend versus include?
Use prepend when you need your module's methods to override the class methods. Use include when you want the class methods to take precedence over the module's methods.
Do refinements completely solve monkey-patching problems?
They help significantly by containing changes to specific scopes, but they're not a silver bullet. The using keyword's lexical scope can sometimes be surprising in complex codebases.
What happened to Ruby 1.8.7 support?
Ruby 2.0 dropped support for version 1.8.7. This was a major cleanup that allowed removal of legacy code and adoption of modern features.
Is the UTF-8 default encoding applied to all files?
Yes, source files are interpreted as UTF-8 by default unless you specify otherwise with a magic comment. This simplifies working with international characters.