What Is New in Ruby 3.1
| Category | Key Changes |
|---|---|
| Language Features | Anonymous block forwarding, Pin operator in pattern matching |
| Core Classes | New File.symlink method, String#unpack updates, Time improvements |
| Performance | YJIT: New experimental in-process JIT compiler |
| Tooling & Debugging | IRB autocomplete and visual indentation, Error highlight improvements |
| Standard Libraries | New Net::Protocol::BufferedIO class, RubyGems and bundler updates |
What are the new language features in Ruby 3.1?
Ruby 3.1 introduces anonymous block forwarding, simplifying the delegation of blocks. You can now use a leading &; argument to forward an anonymous block, which cleans up repetitive code in DSLs and wrappers.
The pin operator in pattern matching now supports expressions. This lets you match against the value of a variable or a constant, not just literals, making pattern matching more dynamic and powerful.
def logged_execution(&;)
log_start
result = yield
log_end(result)
result
end
How has performance improved with YJIT?
YJIT is a new, experimental in-process JIT compiler that can provide significant performance boosts for many workloads. It's designed to be memory-efficient and is supported on x86-64 and arm64/aarch64 CPUs on Unix platforms.
In practice, you'll see the biggest gains on Rails applications and other production workloads. You can enable it with the --yjit command-line flag to test its impact on your specific application.
What core class updates should developers know about?
The File.symlink method was added, providing a direct way to create symbolic links. The String#unpack and String#unpack1 methods now handle the offset keyword argument, offering more control over binary data parsing.
Time parsing became more strict to avoid confusion between local time and UTC. This change prevents subtle bugs that could occur when a time string contains an ambiguous time zone indicator.
# Creating a symlink
File.symlink('target.txt', 'link.txt')
How has the debugging experience been enhanced?
IRB, Ruby's interactive shell, received major quality-of-life improvements. It now features enhanced autocompletion and visual indentation guides, making it much easier to write and debug multi-line code directly in the console.
Error highlighting in the error_highlight gem became the default. It now points more precisely to the exact method argument that caused a NoMatchingPatternError or NoMatchingPatternKeyError, speeding up debugging.
What changes were made to standard libraries?
A new class, Net::Protocol::BufferedIO, was extracted from Net::HTTP for internal use. The RubyGems and bundler were updated to their latest versions, which include various dependency resolution and security improvements.
Several less-used standard libraries like dbm, gdbm, and tracer are now bundled as default gems. This means they are still included but are now versioned and updated independently of the main Ruby release.
FAQ
Is YJIT ready for production use?
YJIT is still marked as experimental. While it shows impressive performance gains on many applications, you should benchmark your specific workload with it enabled before deploying it to critical production environments.
What does anonymous block forwarding actually do?
It allows you to pass along a block to another method without explicitly naming it. This eliminates the need for the old &block syntax and subsequent block.call pattern in many delegation scenarios, making code cleaner.
Do I need to change my code for the new Time parsing strictness?
You might if your code relies on parsing time strings with ambiguous zone formats. The change was made to prevent silent errors, so it's a good practice to ensure your time strings are explicitly formatted to avoid any unexpected behavior.
How do I enable the new IRB features?
The new autocomplete and indentation features are enabled by default in IRB for Ruby 3.1. Just run irb and you'll see the improved interface immediately.
What happened to the debugger?
The legacy debugger (lib/debug.rb) was removed from the standard library. The community is focused on improving the debug.gem, which is the modern, feature-rich alternative that you should use for debugging.