What Is New in Ruby 2.7
Ruby 2.7 introduces significant new features and refinements, focusing on developer productivity and paving the way for Ruby 3. Here's a high-level summary of the key changes.
| Category | Key Changes |
|---|---|
| New Features | Pattern Matching, Numbered Parameters, Beginless Ranges |
| Improvements | Interactive REPL (IRB), Garbage Collector, Module#const_source_location |
| Deprecations | Keyword Arguments, $SAFE, taint/trust |
| Standard Library | New JSON, Reline, and Bundler 2.1+ integration |
How does pattern matching work in Ruby 2.7?
Pattern matching is a major new feature, allowing you to destructure complex data structures like hashes and arrays. It uses the => operator, often called the "match operator," or the in keyword within a case statement.
In practice, this makes extracting nested values much cleaner than using multiple lines of conditional checks and assignments. It feels similar to pattern matching in functional languages but integrated into Ruby's syntax.
Example with Hash
case {name: 'Alice', children: [{name: 'Bob'}]}
in {name: name, children: [{name: child_name}]}
puts "#{name} has a child named #{child_name}"
end
# Output: Alice has a child named Bob
What are numbered parameters for blocks?
Numbered parameters provide a concise way to reference block arguments without explicitly naming them. You use _1, _2, and so on to refer to the first, second, and subsequent parameters.
This is great for short, one-line blocks where defining named parameters feels too verbose. It's a small syntax sugar that speeds up writing simple operations.
# Traditional way
[1, 2, 3].map { |num| num * 2 }
# With numbered parameters
[1, 2, 3].map { _1 * 2 }
How were keyword arguments changed?
Ruby 2.7 introduced a major deprecation warning for the automatic conversion of keyword arguments and a final Hash argument. This change was a crucial step towards the complete separation of positional and keyword arguments in Ruby 3.0.
If your method expects keywords but receives a Hash, you'll see a warning. You need to explicitly use the double splat operator (**) to convert a Hash into keyword arguments.
# This will now generate a warning in 2.7
def my_method(key: nil); end
my_method({key: 42})
# The correct way is to use **
my_method(**{key: 42})
What improvements were made to IRB?
The Interactive Ruby (IRB) console received a massive upgrade with support for multi-line editing, syntax highlighting, and inline command history. It's powered by the new reline library, a pure-Ruby replacement for readline.
This makes the development experience significantly smoother. You can now edit code in the console much more easily, making it a more practical tool for experimentation and debugging.
What other syntax additions are there?
Beginless ranges and a new method for finding constant sources were also added. Beginless ranges let you express "everything from the start up to a point," which is useful for array slicing.
Module#const_source_location is a powerful tool for metaprogramming and debugging, as it tells you exactly where a constant was defined.
Beginless Range Example
# Get all elements up to index 2
array = [1, 2, 3, 4, 5]
array[..2] # => [1, 2, 3]
FAQ
Will my code break because of the keyword argument changes?
Not immediately. Ruby 2.7 issues warnings for problematic code to prepare you for Ruby 3.0. You should address these warnings to ensure a smooth transition, but your existing code will still run.
Is pattern matching just for case statements?
No, you can use the one-line => operator for simple matches. The in operator is also available outside of case statements and will raise a NoMatchingPatternError if the match fails.
Are numbered parameters mandatory?
Absolutely not. They are entirely optional syntax sugar for shortening simple blocks. You can continue to use explicitly named block parameters if you prefer clarity over brevity.
What happened to the $SAFE feature?
The $SAFE global variable and related taint/trust mechanisms have been deprecated and are now a normal no-op global variable. This feature was complex and rarely used effectively.
Is the new IRB enabled by default?
Yes, when you run irb from the command line in Ruby 2.7, you get the new enhanced version automatically. The old functionality is still available via IRB.conf[:USE_READLINE] = false if needed.