2.7.8

Latest release in branch 2.7
Released 3 years ago (March 30, 2023)

Software Ruby
Branch 2.7
End of bug fixes April 01, 2022
End of life March 31, 2023
First official release version 2.7.0
First official release date 6 years ago (December 25, 2019)
Release notes https://ruby-lang.org/en/news/2023/03/30/ruby-2-7-8-released/
Source code https://github.com/ruby/ruby/tree/v2_7_8
Documentation https://ruby-doc.org
Download https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.8.tar.gz
Ruby 2.7 Releases View full list

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.

Releases In Branch 2.7

Version Release date
2.7.8 3 years ago
(March 30, 2023)
2.7.7 3 years ago
(November 24, 2022)
2.7.6 4 years ago
(April 12, 2022)
2.7.5 4 years ago
(November 24, 2021)
2.7.4 4 years ago
(July 07, 2021)
2.7.3 5 years ago
(April 05, 2021)
2.7.2 5 years ago
(October 02, 2020)
2.7.1 6 years ago
(March 31, 2020)
2.7.0 6 years ago
(December 25, 2019)
2.7.0-rc2 6 years ago
(December 21, 2019)
2.7.0-rc1 6 years ago
(December 17, 2019)
2.7.0-preview3 6 years ago
(November 23, 2019)
2.7.0-preview2 6 years ago
(October 22, 2019)
2.7.0-preview1 6 years ago
(May 30, 2019)