Ruby 4.0.0 Released | Ruby

🚀 Discover this trending post from Hacker News 📖

📂 Category:

💡 Here’s what you’ll learn:

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:

      if condition1
         && condition2
        ...
      end
    

    Previously:

      if condition1 && condition2
        ...
      end
    
      if condition1 &&
         condition2
        ...
      end
    

    [Feature #20925]

Core classes updates

Note: We’re only listing outstanding class updates.

  • Array

    • 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

    • Binding#local_variables 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

    • Enumerator.produce now accepts an optional size 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.

        # 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: -> 🔥) Share your opinion below!
        traverser.size  # => 4
      

      [Feature #21701]

  • ErrorHighlight

    • When an ArgumentError is raised, it now displays code snippets for
      both the method call (caller) and the method definition (callee).
      [Feature #21543]

      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 '
      '
  • Fiber

    • Introduce support for Fiber#raise(cause:) argument similar to
      Kernel#raise. [Feature #21360]
  • Fiber::Scheduler

    • Introduce Fiber::Scheduler#fiber_interrupt 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

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

    • IO.select accepts Float::INFINITY as a timeout argument.
      [Feature #20610]

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

  • Kernel

    • Kernel#inspect now checks for the existence of a #instance_variables_to_inspect method,
      allowing control over which instance variables are displayed in the #inspect string:

        class DatabaseConfig
          def initialize(host, user, password)
            @host = host
            @user = user
            @password = password
          end
      
          private def instance_variables_to_inspect = [:@host, :@user]
        end
      
        conf = DatabaseConfig.new("localhost", "root", "hunter2")
        conf.inspect #=> #<0x0000000104def350/>

⚡ Tell us your thoughts in comments!

#️⃣ #Ruby #4.0.0 #Released #Ruby

🕒 Posted on 1766641805

By

Leave a Reply

Your email address will not be published. Required fields are marked *