What’s new in Ruby 3.1 and Rails 7

Agustín Serena
Unagi
Published in
5 min readMar 2, 2022

Every December 25th the Ruby community usually releases a minor/major release and last Christmas was not an exception. Both Ruby 3.1 and Rails 7 were released.
In this post, we are going to review the most important changes that these new versions bring to us.

Illustration by Rosina Gavrilash from Ouch!

Ruby 3.1

YJIT: New JIT compiler (experimental)

Ruby 3.1 adds YJIT, a new in-process JIT compiler developed by Shopify, one of the biggest contributors to Ruby development. This new compiler is designed to improve the performance of our apps, especially those that use Rails, although it’s important to clarify that it’s still in an experimental phase and is disabled by default. In case we want to enable it, we can do it with the –yjit flag.

New debugger: debug gem

Let’s say goodbye to byebug. The new version of Ruby brings a completely new gem to debug our applications: debug gem. Among some of the features of this new gem we can find performance improvements, support for remote debugging, improved debugging interface (only with VS Code and Google Chrome at the moment), multi-process/multi-thread support, colors, and more.

Fine-grain errors in the backtrace: error_highlight

This new gem helps to detect the location of the errors that appear in our console, although at the moment it only supports NameError.

IRB with Autocomplete and Documentation

The IRB console now has a new autocomplete function where a list with suggestions appears, and in addition, the documentation (if exists) of the selected option will appear.

IRB autocomplete & documentation example

Skipping values in hashes and methods with keyword arguments

From now on, it will not be necessary to pass values to a hash or a method with named arguments when variables defined with the same name already exist within the context.

Hash example

Rails 7

Goodbye to Node and Webpack

The new version of Rails no longer requires javascript with NodeJS and Webpack (webpacker gem deprecated). In its replacement, Rails will start using the importmap-rails gem by default.

This way we will no longer have the package.json file or install our dependencies via npm or yarn. From now on, we will have the file config/importmap.rb which will be a kind of Gemfile with all the dependencies, and we will use the “pin” or “unpin” command to install or remove them.

./bin/importmap pin dependency_name
./bin/importmap unpin dependency_name

Turbolinks and UJS are replaced by Turbo and Stimulus

Turbo and Stimulus come by default with Rails 7 apps. These two new players are part of Hotwire, Rails’ brand new solution aimed at reducing Javascript usage by sending HTML instead of JSON over the wire. Hotwire can improve the initial load speed of the app by reducing client-side processing since most of the site is rendering is done on the server-side.

Debug gem by default

As we said above, Ruby 3.1 has a new debugging gem, and Rails 7 will add it by default to replace byebug.

Encrypted attributes

In Rails 7 we can encrypt our database columns very easily through the new encryption method built into ActiveRecord.

Ok, everything looks rosy but this has a small problem, we can’t query using those columns. The solution is to add the “deterministic: true” option to the encrypts method.

A new way to send files from the controllers

How many times did you try to send a file from the controller and the page doesn’t load until the rendering is done? Although there are some solutions to this problem, Rails 7 introduces the send_stream method, which causes the page to return an immediate response while the file continues to be generated.

Ensuring a single record with “sole”

sole is a new method that avoids having to do ".where" + ".first" or ".find_by" when we do a query and need to make sure that only one record exists for that query.

In both queries, the following can happen:

  • If there is only one $100 product, it returns the product in question.
  • If there is no $100 product it throws ActiveRecord::RecordNotFound exception.
  • If there is more than one $100 product it throws ActiveRecord::SoleRecordExceeded exception.

Verifying the presence of an association

With the "associated" method we can check that a record has a certain association. This will save us from having to do "joins" and check for the presence of an ID.

Asynchronous queries

Rails 7 introduces a new method called "load_async" that allows us to run queries in the background.

This allows us to decrease the response time since "load_async" starts a new thread to execute each query, therefore, they are executed simultaneously and immediately. In case we don’t use "load_async" (as we usually do) the queries are not executed until they are referenced in the view.

Unlimited retries in ActiveJob

I’m not sure how useful and efficient this new ActiveJob feature can be, but now we can do unlimited retries of a job with the ":unlimited" option.

I hope you liked these new features as much as I did, and don’t hesitate to comment on any other new features that I haven’t mentioned in this post.

If you liked this article, you may be interested in one of these:

--

--