Skip to content

Conversation

@depfu
Copy link
Contributor

@depfu depfu bot commented Jan 2, 2026

Here is everything you need to know about this upgrade. Please take a good look at what changed and the test results before merging this pull request.

What changed?

Release Notes

4.0.0

Posted by naruse on 25 Dec 2025

We are pleased to announce the release of Ruby 4.0.0. Ruby 4.0 introduces “Ruby Box” and “ZJIT”, and adds many improvements.

Ruby Box

Ruby Box is a new (experimental) feature to provide separation about definitions. Ruby Box is enabled when an environment variable RUBY_BOX=1 is specified. The class is Ruby::Box.

Definitions loaded in a box are isolated in the box. Ruby Box can isolate/separate monkey patches, changes of global/class variables, class/module definitions, and loaded native/ruby libraries from other boxes.

Expected use cases are:

  • Run test cases in box to protect other tests when the test case uses monkey patches to override something
  • Run web app boxes in parallel to execute blue-green deployment on an app server in a Ruby process
  • Run web app boxes in parallel to evaluate dependency updates for a certain period of time by checking response diff using Ruby code
  • Used as the foundation (low-level) API to implement kind of “package” (high-level) API (it is not designed yet)

For the detail of “Ruby Box”, see Ruby::Box. [Feature #21311] [Misc #21385]

ZJIT

ZJIT is a new just-in-time (JIT) compiler, which is developed as the next generation of YJIT. You need Rust 1.85.0 or newer to build Ruby with ZJIT support, and ZJIT is enabled when --zjit is specified.

We’re building a new compiler for Ruby because we want to both raise the performance ceiling (bigger compilation unit size and SSA IR) and encourage more outside contribution (by becoming a more traditional method compiler). See our blog post for more details.

ZJIT is faster than the interpreter, but not yet as fast as YJIT. We encourage you to experiment with ZJIT, but maybe hold off on deploying it in production for now. Stay tuned for Ruby 4.1 ZJIT.

Ractor Improvements

Ractor, Ruby’s parallel execution mechanism, has received several improvements. A new class, Ractor::Port, was introduced to address issues related to message sending and receiving (see our blog post). Additionally, Ractor.shareable_proc makes it easier to share Proc objects between Ractors.

On the performance side, many internal data structures have been improved to significantly reduce contention on a global lock, unlocking better parallelism. Ractors also now share less internal data, resulting in less CPU cache contention when running in parallel.

Ractor was first introduced in Ruby 3.0 as an experimental feature. We aim to remove its “experimental” status next year.

Language changes

  • *nil no longer calls nil.to_a, similar to how **nil does not call nil.to_hash. [Feature #21047]

  • Logical binary operators (||, &&, and and or) at the beginning of a line continue the previous line, like fluent dot. The following code examples are equal:

    <div class="language-ruby highlighter-rouge">
    
      if condition1
         && condition2
        ...
      end
    
    <p>Previously:</p>
    
    <div class="language-ruby highlighter-rouge">
    
      if condition1 && condition2
        ...
      end
    
    <div class="language-ruby highlighter-rouge">
    
      if condition1 &&
         condition2
        ...
      end
    
    <p>[<a href="https://bugs.ruby-lang.org/issues/20925">Feature #20925</a>]</p>
    

Core classes updates

Note: We’re only listing outstanding class updates.

  • Array

    <ul>
      <li>
    

    Array#rfind has been added as a more efficient alternative to array.reverse_each.find [Feature #21678]



  • Array#find has been added as a more efficient override of Enumerable#find [Feature #21678]

  • Binding

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Binding#local_variables</code> does no longer include numbered parameters.
    

    Also, Binding#local_variable_get, Binding#local_variable_set, and
    Binding#local_variable_defined? reject to handle numbered parameters.
    [Bug #21049]




  • Binding#implicit_parameters, Binding#implicit_parameter_get, and
    Binding#implicit_parameter_defined? have been added to access
    numbered parameters and “it” parameter. [Bug #21049]



  • Enumerator

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Enumerator.produce</code> now accepts an optional <code class="language-plaintext highlighter-rouge">size</code> keyword argument
    

    to specify the size of the enumerator. It can be an integer,
    Float::INFINITY, a callable object (such as a lambda), or nil to
    indicate unknown size. When not specified, the size defaults to
    Float::INFINITY.

        <div class="language-ruby highlighter-rouge">
    
      # Infinite enumerator
      enum = Enumerator.produce(1, size: Float::INFINITY, &:succ)
      enum.size  # => Float::INFINITY
    

    # Finite enumerator with known/computable size
    abs_dir = File.expand_path("./baz") # => "/foo/bar/baz"
    traverser = Enumerator.produce(abs_dir, size: -> { abs_dir.count("/") + 1 }) {
    raise StopIteration if it == "/"
    File.dirname(it)
    }
    traverser.size # => 4

        <p>[<a href="https://bugs.ruby-lang.org/issues/21701">Feature #21701</a>]</p>
      </li>
    </ul>
    
  • ErrorHighlight

    <ul>
      <li>
        <p>When an ArgumentError is raised, it now displays code snippets for
    

    both the method call (caller) and the method definition (callee).
    [Feature #21543]

        <div class="language-plaintext highlighter-rouge">
    
    test.rb:1:in 'Object#add': wrong number of arguments (given 1, expected 2) (ArgumentError)
    
    caller: test.rb:3
    | add(1)
      ^^^
    callee: test.rb:1
    | def add(x, y) = x + y
          ^^^
        from test.rb:3:in '&lt;main&gt;'
    



  • Fiber

    <ul>
      <li>Introduce support for <code class="language-plaintext highlighter-rouge">Fiber#raise(cause:)</code> argument similar to
    

    Kernel#raise. [Feature #21360]


  • Fiber::Scheduler

    <ul>
      <li>
        <p>Introduce <code class="language-plaintext highlighter-rouge">Fiber::Scheduler#fiber_interrupt</code> to interrupt a fiber with a
    

    given exception. The initial use case is to interrupt a fiber that is
    waiting on a blocking IO operation when the IO operation is closed.
    [Feature #21166]




  • Introduce Fiber::Scheduler#yield to allow the fiber scheduler to
    continue processing when signal exceptions are disabled.
    [Bug #21633]




  • Reintroduce the Fiber::Scheduler#io_close hook for asynchronous IO#close.




  • Invoke Fiber::Scheduler#io_write when flushing the IO write buffer.
    [Bug #21789]



  • File

    <ul>
      <li>
    

    File::Stat#birthtime is now available on Linux via the statx
    system call when supported by the kernel and filesystem.
    [Feature #21205]


  • IO

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">IO.select</code> accepts <code class="language-plaintext highlighter-rouge">Float::INFINITY</code> as a timeout argument.
    

    [Feature #20610]




  • A deprecated behavior, process creation by IO class methods
    with a leading |, was removed. [Feature #19630]



  • Kernel

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Kernel#inspect</code> now checks for the existence of a <code class="language-plaintext highlighter-rouge">#instance_variables_to_inspect</code> method,
    

    allowing control over which instance variables are displayed in the #inspect string:

        <div class="language-ruby highlighter-rouge">
    
      class DatabaseConfig
        def initialize(host, user, password)
          @host = host
          @user = user
          @password = password
        end
    
    <span class="kp">private</span> <span class="k">def</span> <span class="nf">instance_variables_to_inspect</span> <span class="o">=</span> <span class="p">[</span><span class="ss">:@host</span><span class="p">,</span> <span class="ss">:@user</span><span class="p">]</span>
    

    end

    conf = DatabaseConfig.new("localhost", "root", "hunter2")
    conf.inspect #=> #<DatabaseConfig:0x0000000104def350 @host="localhost", @user="root">

        <p>[<a href="https://bugs.ruby-lang.org/issues/21219">Feature #21219</a>]</p>
      </li>
      <li>
        <p>A deprecated behavior, process creation by <code class="language-plaintext highlighter-rouge">Kernel#open</code> with a
    

    leading |, was removed. [Feature #19630]



  • Math

    <ul>
      <li>
    

    Math.log1p and Math.expm1 are added. [Feature #21527]


  • Pathname

    <ul>
      <li>Pathname has been promoted from a default gem to a core class of Ruby.
    

    [Feature #17473]


  • Proc

    <ul>
      <li>
    

    Proc#parameters now shows anonymous optional parameters as [:opt]
    instead of [:opt, nil], making the output consistent with when the
    anonymous parameter is required. [Bug #20974]


  • Ractor

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Ractor::Port</code> class was added for a new synchronization mechanism
    

    to communicate between Ractors. [Feature #21262]

        <div class="language-ruby highlighter-rouge">
    
      port1 = Ractor::Port.new
      port2 = Ractor::Port.new
      Ractor.new port1, port2 do |port1, port2|
        port1 << 1
        port2 << 11
        port1 << 2
        port2 << 12
      end
      2.times{ p port1.receive } #=> 1, 2
      2.times{ p port2.receive } #=> 11, 12
    
        <p><code class="language-plaintext highlighter-rouge">Ractor::Port</code> provides the following methods:</p>
    
        <ul>
          <li><code class="language-plaintext highlighter-rouge">Ractor::Port#receive</code></li>
          <li>
    

    Ractor::Port#send (or Ractor::Port#<<)


  • Ractor::Port#close

  • Ractor::Port#closed?

  •     <p>As a result, <code class="language-plaintext highlighter-rouge">Ractor.yield</code> and <code class="language-plaintext highlighter-rouge">Ractor#take</code> were removed.</p>
      </li>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Ractor#join</code> and <code class="language-plaintext highlighter-rouge">Ractor#value</code> were added to wait for the
    

    termination of a Ractor. These are similar to Thread#join
    and Thread#value.




  • Ractor#monitor and Ractor#unmonitor were added as low-level
    interfaces used internally to implement Ractor#join.




  • Ractor.select now only accepts Ractors and Ports. If Ractors are given,
    it returns when a Ractor terminates.




  • Ractor#default_port was added. Each Ractor has a default port,
    which is used by Ractor.send, Ractor.receive.




  • Ractor#close_incoming and Ractor#close_outgoing were removed.




  • Ractor.shareable_proc and Ractor.shareable_lambda are introduced
    to make shareable Proc or lambda.
    [Feature #21550], [Feature #21557]



  • Range

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Range#to_set</code> now performs size checks to prevent issues with
    

    endless ranges. [Bug #21654]




  • Range#overlap? now correctly handles infinite (unbounded) ranges.
    [Bug #21185]




  • Range#max behavior on beginless integer ranges has been fixed.
    [Bug #21174] [Bug #21175]



  • Ruby

    <ul>
      <li>A new toplevel module <code class="language-plaintext highlighter-rouge">Ruby</code> has been defined, which contains
    

    Ruby-related constants. This module was reserved in Ruby 3.4
    and is now officially defined. [Feature #20884]


  • Ruby::Box

    <ul>
      <li>A new (experimental) feature to provide separation about definitions.
    

    For the detail of “Ruby Box”, see doc/language/box.md.
    [Feature #21311] [Misc #21385]


  • Set

    <ul>
      <li>
        <p><code class="language-plaintext highlighter-rouge">Set</code> is now a core class, instead of an autoloaded stdlib class.
    

    [Feature #21216]




  • Set#inspect now uses a simpler display, similar to literal arrays.
    (e.g., Set[1, 2, 3] instead of #<Set: {1, 2, 3}>). [Feature #21389]




  • Passing arguments to Set#to_set and Enumerable#to_set is now deprecated.
    [Feature #21390]



  • Socket

    <ul>
      <li>
    

    Socket.tcp & TCPSocket.new accepts an open_timeout keyword argument to specify
    the timeout for the initial connection. [Feature #21347]


  • When a user-specified timeout occurred in TCPSocket.new, either Errno::ETIMEDOUT
    or IO::TimeoutError could previously be raised depending on the situation.
    This behavior has been unified so that IO::TimeoutError is now consistently raised.
    (Please note that, in Socket.tcp, there are still cases where Errno::ETIMEDOUT
    may be raised in similar situations, and that in both cases Errno::ETIMEDOUT may be
    raised when the timeout occurs at the OS level.)

  • String

    <ul>
      <li>
        <p>Update Unicode to Version 17.0.0 and Emoji Version 17.0.
    

    [Feature #19908][Feature #20724][Feature #21275] (also applies to Regexp)




  • String#strip, strip!, lstrip, lstrip!, rstrip, and rstrip!
    are extended to accept *selectors arguments. [Feature #21552]



  • Thread

    <ul>
      <li>Introduce support for <code class="language-plaintext highlighter-rouge">Thread#raise(cause:)</code> argument similar to
    

    Kernel#raise. [Feature #21360]


  • Stdlib updates

    We only list stdlib changes that are notable feature changes.

    Other changes are listed in the following sections. We also listed release history from the previous bundled version that is Ruby 3.4.0 if it has GitHub releases.

    The following bundled gems are promoted from default gems.

    The following default gem is added.

    • win32-registry 0.1.2

    The following default gems are updated.

    The following bundled gems are updated.

    RubyGems and Bundler

    Ruby 4.0 bundled RubyGems and Bundler version 4. see the following links for details.

    Supported platforms

    • Windows

      <ul>
        <li>Dropped support for MSVC versions older than 14.0 (_MSC_VER 1900).
      

      This means Visual Studio 2015 or later is now required.


    Compatibility issues

    • The following methods were removed from Ractor due to the addition of Ractor::Port:

      <ul>
        <li><code class="language-plaintext highlighter-rouge">Ractor.yield</code></li>
        <li><code class="language-plaintext highlighter-rouge">Ractor#take</code></li>
        <li><code class="language-plaintext highlighter-rouge">Ractor#close_incoming</code></li>
        <li><code class="language-plaintext highlighter-rouge">Ractor#close_outgoing</code></li>
      </ul>
      
      <p>[<a href="https://bugs.ruby-lang.org/issues/21262">Feature #21262</a>]</p>
      
    • ObjectSpace._id2ref is deprecated. [Feature #15408]

    • Process::Status#& and Process::Status#>> have been removed. They were deprecated in Ruby 3.3. [Bug #19868]

    • rb_path_check has been removed. This function was used for $SAFE path checking which was removed in Ruby 2.7, and was already deprecated. [Feature #20971]

    • A backtrace for ArgumentError of “wrong number of arguments” now include the receiver’s class or module name (e.g., in Foo#bar instead of in bar). [Bug #21698]

    • Backtraces no longer display internal frames. These methods now appear as if it is in the Ruby source file, consistent with other C-implemented methods. [Bug #20968]

      <p>Before:</p>
      <div class="language-plaintext highlighter-rouge">
      
      ruby -e '[1].fetch_values(42)'
      <internal:array>:211:in 'Array#fetch': index 42 outside of array bounds: -1...1 (IndexError)
              from <internal:array>:211:in 'block in Array#fetch_values'
              from <internal:array>:211:in 'Array#map!'
              from <internal:array>:211:in 'Array#fetch_values'
              from -e:1:in '<main>'
      
      <p>After:</p>
      <div class="language-plaintext highlighter-rouge">
      
      $ ruby -e '[1].fetch_values(42)'
      -e:1:in 'Array#fetch_values': index 42 outside of array bounds: -1...1 (IndexError)
              from -e:1:in '<main>'
      

    Stdlib compatibility issues

    • CGI library is removed from the default gems. Now we only provide cgi/escape for the following methods:

      <ul>
        <li>
      

      CGI.escape and CGI.unescape

    • CGI.escapeHTML and CGI.unescapeHTML
    • CGI.escapeURIComponent and CGI.unescapeURIComponent
    • CGI.escapeElement and CGI.unescapeElement
    <p>[<a href="https://bugs.ruby-lang.org/issues/21258">Feature #21258</a>]</p>
    
  • With the move of Set from stdlib to core class, set/sorted_set.rb has been removed, and SortedSet is no longer an autoloaded constant. Please install the sorted_set gem and require 'sorted_set' to use SortedSet. [Feature #21287]

  • Net::HTTP

    <ul>
      <li>The default behavior of automatically setting the <code class="language-plaintext highlighter-rouge">Content-Type</code> header
    

    to application/x-www-form-urlencoded for requests with a body
    (e.g., POST, PUT) when the header was not explicitly set has been
    removed. If your application relied on this automatic default, your
    requests will now be sent without a Content-Type header, potentially
    breaking compatibility with certain servers.
    [GH-net-http #205]


  • C API updates

    • IO

      <ul>
        <li>
      

      rb_thread_fd_close is deprecated and now a no-op. If you need to expose
      file descriptors from C extensions to Ruby code, create an IO instance
      using RUBY_IO_MODE_EXTERNAL and use rb_io_close(io) to close it (this
      also interrupts and waits for all pending operations on the IO
      instance). Directly closing file descriptors does not interrupt pending
      operations, and may lead to undefined behaviour. In other words, if two
      IO objects share the same file descriptor, closing one does not affect
      the other. [Feature #18455]


  • GVL

    <ul>
      <li>
    

    rb_thread_call_with_gvl now works with or without the GVL.
    This allows gems to avoid checking ruby_thread_has_gvl_p.
    Please still be diligent about the GVL. [Feature #20750]


  • Set

    <ul>
      <li>
        <p>A C API for <code class="language-plaintext highlighter-rouge">Set</code> has been added. The following methods are supported:
    

    [Feature #21459]

        <ul>
          <li><code class="language-plaintext highlighter-rouge">rb_set_foreach</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_new</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_new_capa</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_lookup</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_add</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_clear</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_delete</code></li>
          <li><code class="language-plaintext highlighter-rouge">rb_set_size</code></li>
        </ul>
      </li>
    </ul>
    
  • Implementation improvements

    • Class#new (ex. Object.new) is faster in all cases, but especially when passing keyword arguments. This has also been integrated into YJIT and ZJIT. [Feature #21254]
    • GC heaps of different size pools now grow independently, reducing memory usage when only some pools contain long-lived objects
    • GC sweeping is faster on pages of large objects
    • “Generic ivar” objects (String, Array, TypedData, etc.) now use a new internal “fields” object for faster instance variable access
    • The GC avoids maintaining an internal id2ref table until it is first used, making object_id allocation and GC sweeping faster
    • object_id and hash are faster on Class and Module objects
    • Larger bignum Integers can remain embedded using variable width allocation
    • Random, Enumerator::Product, Enumerator::Chain, Addrinfo, StringScanner, and some internal objects are now write-barrier protected, which reduces GC overhead.

    Ractor

    A lot of work has gone into making Ractors more stable, performant, and usable. These improvements bring Ractor implementation closer to leaving experimental status.

    • Performance improvements
      • Frozen strings and the symbol table internally use a lock-free hash set [Feature #21268]
      • Method cache lookups avoid locking in most cases
      • Class (and generic ivar) instance variable access is faster and avoids locking
      • CPU cache contention is avoided in object allocation by using a per-ractor counter
      • CPU cache contention is avoided in xmalloc/xfree by using a thread-local counter
      • object_id avoids locking in most cases
    • Bug fixes and stability
      • Fixed possible deadlocks when combining Ractors and Threads
      • Fixed issues with require and autoload in a Ractor
      • Fixed encoding/transcoding issues across Ractors
      • Fixed race conditions in GC operations and method invalidation
      • Fixed issues with processes forking after starting a Ractor
      • GC allocation counts are now accurate under Ractors
      • Fixed TracePoints not working after GC [Bug #19112]

    JIT

    • ZJIT
      • Introduce an experimental method-based JIT compiler. Where available, ZJIT can be enabled at runtime with the --zjit option or by calling RubyVM::ZJIT.enable. When building Ruby, Rust 1.85.0 or later is required to include ZJIT support.
      • As of Ruby 4.0.0, ZJIT is faster than the interpreter, but not yet as fast as YJIT. We encourage experimentation with ZJIT, but advise against deploying it in production for now.
      • Our goal is to make ZJIT faster than YJIT and production-ready in Ruby 4.1.
    • YJIT
      • RubyVM::YJIT.runtime_stats
        • ratio_in_yjit no longer works in the default build. Use --enable-yjit=stats on configure to enable it on --yjit-stats.
        • Add invalidate_everything to default stats, which is incremented when every code is invalidated by TracePoint.
      • Add mem_size: and call_threshold: options to RubyVM::YJIT.enable.
    • RJIT
      • --rjit is removed. We will move the implementation of the third-party JIT API to the ruby/rjit repository.

    See NEWS or commit logs for more details.

    With those changes, 3889 files changed, 230769 insertions(+), 297003 deletions(-) since Ruby 3.4.0!

    Merry Christmas, a Happy New Year, and Happy Hacking with Ruby 4.0!


    All Depfu comment commands
    @​depfu refresh
    Rebases against your default branch and redoes this update
    @​depfu recreate
    Recreates this PR, overwriting any edits that you've made to it
    @​depfu merge
    Merges this PR once your tests are passing and conflicts are resolved
    @​depfu close
    Closes this PR and deletes the branch
    @​depfu reopen
    Restores the branch and reopens this PR (if it's closed)
    @​depfu pause
    Pauses all engine updates and closes this PR

    @depfu depfu bot added the depfu label Jan 2, 2026
    @depfu depfu bot assigned mockdeep Jan 2, 2026
    @depfu depfu bot requested a review from mockdeep January 2, 2026 00:01
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants