Ruby 3.1 Introduces Fine-grained error location backtrace

Backtrace is very important when exceptions are raised. The sequential representation enables programmers to trace the exact location in their code where these exceptions are raised.

Ruby 3.1 ships with the error_highlight gem out of the box. This gem includes a fine-grained error location in the backtrace. The gem only works for Ruby 3.1 and later versions because it depends on MRI and its internal APIs that are only available since version 3.1.

Take into consideration the Ruby code below:

1
1.time {}

Before Ruby 3.1

1
2
3
4
$ ruby test.rb
test.rb:1:in `<main>': undefined method `time' for 1:Integer (NoMethodError)

Did you mean?  times

After Ruby 3.1

1
2
3
4
5
6
$ ruby test.rb
test.rb:1:in `<main>': undefined method `time' for 1:Integer (NoMethodError)
        
1.time {}
^^^^^
Did you mean?  times

Note: The API is still experimental and is subject to changes in the future.

More about the gem can be found here.