What is New in Ruby 4.0
Ruby 4.0 brings a range of thoughtful updates that improve performance, refine the language syntax, and enhance developer experience while maintaining backward compatibility in most areas. This release focuses on making the runtime faster, Ractors more practical, and error messages clearer, alongside promoting several useful libraries into the core.
Key Language and Core Improvements
The language sees several small but helpful changes. Logical operators like ||, &&, and, and or can now continue from the previous line when placed at the start of a new line, similar to method chaining with dots.
A new top-level module called Ruby is introduced to hold Ruby-related constants. An experimental feature, Ruby::Box, can be enabled with the environment variable RUBY_BOX=1 to isolate monkey patches, global variables, and loaded libraries.
Performance Enhancements
Performance gets a significant boost across the board. A new experimental JIT compiler named ZJIT is introduced, built with Rust, and activated via --zjit. While still early, it shows promise for future speed gains.
YJIT receives further optimizations, including new runtime stats and configuration options like mem_size: and call_threshold:.
Ractor parallelism improves through reduced global lock contention, lock-free data structures for frozen strings and symbols, per-ractor object allocation counters, and faster method and instance variable access. Garbage collection becomes more efficient with independent heap growth per object size pool, faster sweeping, and deferred internal tables.
Common operations like Class#new, object_id, and hash on classes and modules are noticeably quicker.
Ractor Changes
Ractors gain better synchronization tools with the new Ractor::Port class, which supports sending, receiving, and closing. Methods like Ractor#join, Ractor#value, and Ractor.select make working with ractors feel more like threads. Older methods such as Ractor.yield and Ractor#take are removed in favor of these cleaner alternatives.
Core Class Updates
Arrayadds#rfindand an optimized#find.Bindingprovides new ways to handle implicit numbered parameters and the "it" parameter.Enumerator.produceaccepts asizekeyword for better control.ErrorHighlightnow shows code snippets from both caller and callee inArgumentErrormessages.Fibergains#raise(cause:)and scheduler hooks for interrupts and IO operations.Mathaddslog1pandexpm1.PathnameandSetare promoted to core classes, withSetgetting a simpler#inspectformat.Stringupdates to Unicode 17.0 and Emoji 17.0, and strip methods accept selector arguments.Socketsupportsopen_timeoutfor connections.
Standard Library Promotions and Updates
Several libraries move from default gems to bundled core, including ostruct, pstore, benchmark, logger, rdoc, and irb. Many others receive version bumps with bug fixes and improvements.
Error Handling and Debugging
Argument errors now include clearer backtraces with receiver class information and code snippets from both sides of the call. Internal frames are hidden from backtraces for cleaner output.
Compatibility Notes
Some older features are removed, such as piped process creation via Kernel#open or IO methods, and certain Process::Status operators. The CGI library is largely removed, leaving only escape utilities.
Overall, Ruby 4.0 delivers a more efficient and modern runtime while keeping the language familiar and productive.