Skip to content
Tutorials Center
My learning

Topics

Lesson 109 of 134 ยท Testing and Tooling

Ruby Versions and Changes

A tour of Ruby 3.0 through 4.0 - keyword arguments, pattern matching, endless methods, Data, the it parameter, Set in core, Ractor ports, Prism, JITs - with runnable examples and upgrade advice.

Ruby has a predictable rhythm: a new version arrives every December 25th, and each one adds a handful of features and removes a few old things. You do not need to memorize a changelog, but you do need a map. When you read older code, a blog post from 2015, or a library's compatibility table, knowing which Ruby introduced what explains why code looks the way it does and tells you what is safe to use in your project.

This lesson walks from Ruby 3.0 (December 2020) to Ruby 4.0, picking the changes that affect everyday code. Every example below runs on the Ruby 4.0.9 of this site's runner, and the error messages and output formats are the real ones from that version. For a given feature we also say where it came from, so you can judge whether your minimum supported version has it.

How versions work

Ruby versions have the form MAJOR.MINOR.PATCH. Minor releases (3.3, 3.4) are the yearly ones and may bring new features and small incompatibilities; patch releases (3.4.7) only fix bugs and security issues. The major number changes rarely: 3.0 was the big "Ruby 3x3" speed and concurrency goal, and 4.0 is the next milestone. The core team supports each minor version with bug fixes for a couple of years and security fixes a little longer, so running a release that is a few years old means running unsupported software.

Ruby Which Ruby is running?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

As in RubyGems and Bundler, compare versions with Gem::Version, never as strings. In a project you declare the supported Rubies in the gemspec (required_ruby_version = ">= 3.3") or in the Gemfile (ruby "~> 4.0"), and a .ruby-version file tells version managers which one to use (see How Ruby runs).

Ruby 3.0 (2020): the big break

Ruby 3.0 set three goals: faster, concurrent, and typed. The changes you meet daily are these.

Keyword arguments are separate from hashes. Before 3.0, Ruby converted a trailing hash into keyword arguments and the other way round, which caused confusing bugs. Now they are different things. A hash passed where keywords are expected is not converted any more; to pass a hash as keywords you write **hash.

Endless method definitions. A one-expression method can be written with =. Rightward pattern matching (experimental in 3.0, stable in 3.1) expression => pattern raises if the pattern does not match, and expression in pattern returns true or false. Find patterns [*, x, *] search inside arrays. Hash#except joined the core. Ractor (parallel actors) and the Fiber scheduler (for non-blocking I/O) appeared, and RBS (type signatures) was bundled.

Ruby Ruby 3.0: keywords, endless methods, patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Pattern matching is covered fully in Pattern matching, and keyword arguments in Method arguments.

Ruby 3.1 (2021): shorthand and YJIT

  • Hash shorthand: { x:, y: } means { x: x, y: y }. The same works in method calls: point(x:, y:).
  • Anonymous block forwarding: def run(&) = each(&).
  • Pin expressions: inside patterns, ^(expression) matches against a computed value, and ^variable against a local variable.
  • YJIT, the first built-in JIT compiler, arrived (see Performance Tips).
  • Array#intersect?, Enumerable#compact, and the error_highlight gem, which underlines the exact spot in an error message.
Ruby Ruby 3.1: shorthand, pins and intersect?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Ruby 3.2 (2022): Data and anonymous splats

  • Data.define: immutable value objects (see Structs and Data). The inspect output is #<data Name field=value>.
  • Anonymous rest and keyword rest: def wrap(*, **, &) = target(*, **, &) forwards everything without naming it.
  • Set is available without require "set", Integer#ceildiv, Enumerator::Product, Struct accepts keywords by default, and Fixnum/Bignum (deprecated for years) are finally gone.
Ruby Ruby 3.2: Data and argument forwarding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The last two blocks show deliberate failures: a Data instance has no setters, and Fixnum no longer exists (use Integer). Those were the real messages.

Ruby 3.3 (2023): faster and friendlier

3.3 was mostly about performance and tooling: an improved YJIT that can be enabled at runtime with RubyVM::YJIT.enable, a new parser called Prism shipped as a bundled gem (it became the default parser in 3.4), the M:N thread scheduler (many Ruby threads share fewer OS threads, enabled for Ractors), and Range#overlap?. It also began warning about code that called a method named it inside a block, preparing the way for the real it in 3.4.

Ruby 3.4 (2024): it, new inspect and error messages

This version changed output that you can see, which also means older tutorials will show different text.

  • it is the implicit block parameter: list.map { it * 2 } instead of { |n| n * 2 } or _1.
  • Prism is the default parser (Prism.parse is available after require "prism"; see the linter in Style and Linting).
  • Hash#inspect prints {name: "Ada", "key" => 1}: symbol keys as key: value, others with spaces around =>. Before 3.4 it was {:name=>"Ada", "key"=>1}.
  • Error messages use a plain quote ' instead of a backtick and name the class: undefined method 'upcase' for nil, and backtraces say 'Integer#/' instead of `/'.
  • Chilled strings: string literals in files without a magic comment are "chilled": they can still be mutated, but Ruby is preparing to make them frozen (a warning appears when deprecation warnings are on).
  • Array#fetch_values, Hash.new(capacity:), Time#xmlschema in core, and warnings for requiring gems that will leave the default set.
Ruby Ruby 3.4: it, inspect and error text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

If you are upgrading code that compares or parses inspect output or error messages (tests do this surprisingly often), these formats are the first thing that breaks.

Ruby 4.0: Set, Ractors and a second JIT

Ruby 4.0 is the newest release this tutorial covers. The changes most programs notice:

  • Set is a core class (implemented in C, no require and no autoload), and prints as Set[1, 2, 3].
  • Ractor got a new communication API. Ractor.yield and Ractor#take were replaced by Ractor::Port and Ractor#value, and the first Ractor.new still prints an experimental warning. Ractors run Ruby code in parallel without sharing mutable objects (see Ractors).
  • ZJIT, a new method-based JIT, joined YJIT. It is aimed at the future and not yet the default choice.
  • More libraries moved from the default set to bundled gems (csv, base64, bigdecimal, observer, ostruct and others): they still install with Ruby but a Gemfile project must list them.
  • Error messages say for main (not for main:Object), and many errors now append a source excerpt.
  • Ruby::Box is an experimental way to isolate code and monkey patches in separate namespaces; it is off by default.
Ruby Ruby 4.0: Set in core and Ractor values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The Ractor lines also print one warning: Ractor API is experimental message on stderr; it is normal. Ractor results are fetched with value.

Here is the old style failing, in the 4.0 world. This is what an older tutorial's take produces:

Ruby The Ractor take method is gone (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The first line prints 42; the second Ractor fails with undefined method 'take' for an instance of Ractor (NoMethodError). The fix is other.value. When you meet Ractor code from 2021 to 2024, it needs this update.

A feature map

Feature Since Notes
Keyword arguments separated from hashes 3.0 Use **hash to pass a hash as keywords
Endless methods, => and in patterns, find pattern 3.0 One-line patterns were experimental in 3.0 and stable from 3.1
Ractor, Fiber scheduler 3.0 Ractor API changed in 4.0
{ x:, y: } shorthand, & forwarding, pin expressions 3.1
YJIT 3.1 RubyVM::YJIT.enable at runtime from 3.3
Data.define, anonymous * and ** 3.2
Prism parser 3.3 (gem), 3.4 (default)
it 3.4
New Hash#inspect, new error quotes 3.4
Set as a core class, Ractor::Port, ZJIT 4.0

Choosing a version and upgrading safely

  • Start new projects on the newest stable release (4.0 or later); you get the best performance and all the syntax in this tutorial.
  • Libraries support a range: the current release and the previous two or three. Declare it with required_ruby_version, and test all of them in CI.
  • Upgrade one minor version at a time (3.2 to 3.3 to 3.4). Read the release notes, run your tests with warnings on (ruby -W:deprecated), and fix what they report before the removal arrives.
  • Check gems. A gem might not yet support the new Ruby. bundle update and the CI matrix will tell you.
  • Avoid the removed or deprecated. Do not use Fixnum/Bignum, $SAFE, Object#taint, Hash#=~, open with a URL, or rescue Exception; they are gone or warn.

You can also write code that adapts to the running version, which is useful in a library or in a transitional app:

Ruby Feature detection instead of version checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Prefer asking "can this object do X?" (respond_to?, defined?) over comparing version numbers when you can, because it also works on other Ruby implementations such as JRuby and TruffleRuby.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A method is defined as def connect(host, port: 80). In Ruby 3.0 and later, what does connect("h", { port: 1 }) do?

    Keyword arguments and hashes are separate since 3.0. Write connect("h", port: 1) or connect("h", **options).
  2. 2. Which version introduced it as the implicit block parameter?

    it arrived in Ruby 3.4, so [1, 2].map { it * 2 } works from 3.4 on.
  3. 3. What does { name: "Ada", "id" => 1 }.inspect print in Ruby 3.4 and later?

    Symbol keys print as key: value and other keys print with spaces around =>.
  4. 4. In Ruby 4.0, how do you collect the result of a Ractor?

    Ractor#take and Ractor.yield were replaced by Ractor#value and Ractor::Port in 4.0.
  5. 5. What is the safest way to compare Ruby versions in code?

    Gem::Version compares segment by segment as numbers. Strings and floats mis-order versions such as 3.10 and 3.9.

Summary

  • Ruby releases a new minor version every December; patch releases only fix bugs. Compare versions with Gem::Version, never as strings.
  • 3.0: keyword arguments separated from hashes, endless methods, => and in pattern matching, Ractor and the Fiber scheduler.
  • 3.1 and 3.2: { x:, y: } shorthand, pin expressions, YJIT, Data.define, anonymous *, ** and & forwarding; Fixnum and Bignum removed.
  • 3.4: it, Prism as default parser, the new Hash#inspect format and error messages with plain quotes and class names.
  • 4.0: Set in core, Ractor::Port and Ractor#value replacing take and yield, ZJIT, and more bundled gems.
  • Upgrade one version at a time, run tests with warnings on, and prefer feature detection over version checks when you can.

Next: Ruby on Rails Overview