NEWS for Ruby 3.1.0





def foo(&)
Prime.each_cons(2).lazy.find_all{_1 in [n, ^(n + 2)]}.take(3).to_a
#=> [[3, 5], [5, 7], [11, 13]]
@n = 5
Prime.each_cons(2).lazy.find{_1 in [n, ^@n]}
#=> [3, 5]
[0, 1] => _, x
{y: 2} => y:
x #=> 1
y #=> 2

foo[0] = bar

The following evaluation order is used:

1. `foo`
2. `bar`
3. `[]=` called on the result of `foo`

In Ruby before 3.1.0, multiple assignment did not follow this evaluation order. With this code:

foo[0], bar.baz = a, b

Versions of Ruby before 3.1.0 would evaluate in the following order

1. `a`
2. `b`
3. `foo`
4. `[]=` called on the result of `foo`
5. `bar`
6. `baz=` called on the result of `bar`

Starting in Ruby 3.1.0, the evaluation order is now consistent with single assignment, with the left-hand side being evaluated before the right-hand side:

1. `foo`
2. `bar`
3. `a`
4. `b`
5. `[]=` called on the result of `foo`
6. `baz=` called on the result of `bar`

  [[Bug #4443]]

* Values in Hash literals and keyword arguments can be omitted. [[Feature #14579]]

  For example,

  * `{x:, y:}` is a syntax sugar of `{x: x, y: y}`.
  * `foo(x:, y:)` is a syntax sugar of `foo(x: x, y: y)`.

  Constant names, local variable names, and method names are allowed as
  key names.  Note that a reserved word is considered as a local
  variable or method name even if it's a pseudo variable name such as

* Non main-Ractors can get instance variables (ivars) of classes/modules if ivars refer to shareable objects. [[Feature #17592]]

* A command syntax is allowed in endless method definitions, i.e., you can now write `def foo = puts "Hello"`. Note that `private def foo = puts "Hello"` does not parse. [[Feature #17398]]



Note: We're only listing outstanding class updates.

class ;A end
class B < A; end
class C < B; end
class D < A; end
A.subclasses    #=> [D, B]
B.subclasses    #=> [C]
C.subclasses    #=> []

[1, 2, 3].each_cons(2){}
# 3.0 => nil
# 3.1 => [1, 2, 3]

[1, 2, 3].each_slice(2){}
# 3.0 => nil
# 3.1 => [1, 2, 3], 12, 25, in: "+07:00")
#=> 2021-12-25 00:00:00 +0700
At the same time, time component strings are converted to integers more strictly now., 12, 25, "+07:30")
#=> invalid value for Integer(): "+07:30" (ArgumentError)
    Ruby 3.0 or earlier returned probably unexpected result `2021-12-25 07:00:00`, not `2021-12-25 07:30:00` nor `2021-12-25 00:00:00 +07:30`.

  * Time#strftime supports RFC 3339 UTC for unknown offset local time, `-0000`, as `%-z`. [[Feature #17544]]

* TracePoint
  * TracePoint.allow_reentry is added to allow reenter while TracePoint callback. [[Feature #15912]]
  * $LOAD_PATH.resolve_feature_path does not raise. [[Feature #16043]]
* Fiber Scheduler
  * Add support for `Addrinfo.getaddrinfo` using `address_resolve` hook. [[Feature #17370]]
  * Introduce non-blocking `Timeout.timeout` using `timeout_after` hook. [[Feature #17470]]
  * Introduce new scheduler hooks `io_read` and `io_write` along with a low level `IO::Buffer` for zero-copy read/write. [[Feature #18020]]
  * IO hooks `io_wait`, `io_read`, `io_write`, receive the original IO object where possible. [[Bug #18003]]
  * Make `Monitor` fiber-safe. [[Bug #17827]]
  * Replace copy coroutine with pthread implementation. [[Feature #18015]]
* Refinement
  * New class which represents a module created by Module#refine. `include` and `prepend` are deprecated, and `import_methods` is added instead. [[Bug #17429]]



Note: Excluding feature bug fixes.

* `rb_io_wait_readable`, `rb_io_wait_writable` and `rb_wait_for_single_fd` are deprecated in favour of `rb_io_maybe_wait_readable`, `rb_io_maybe_wait_writable` and `rb_io_maybe_wait` respectively. `rb_thread_wait_fd` and `rb_thread_fd_writable` are deprecated. [[Bug #18003]]


C API の更新




YJIT: New experimental in-process JIT compiler

New JIT compiler available as an experimental feature. [[Feature #18229]]

See [this blog post]( introducing the project.



# `T` must be compatible with the `_Output` interface.
# `PrettyPrint[String]` is ok, but `PrettyPrint[Integer]` is a type error.
class PrettyPrint[T < _Output]
  interface _Output
    def <<: (String) -> void

  attr_reader output: T

  def initialize: (T output) -> void
# Defines a generic type `list`.
type list[T] = [ T, list[T] ]
             | nil

type str_list = list[String]
type int_list = list[Integer]

See the []( for more information.




A built-in gem called error_highlight has been introduced. It shows fine-grained error locations in the backtrace.

Example: `title = json[:article][:title]`

If `json` is nil, it shows:

$ ruby test.rb
test.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

title = json[:article][:title]

If `json[:article]` returns nil, it shows:

$ ruby test.rb
test.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

title = json[:article][:title]

This feature is enabled by default. You can disable it by using a command-line option `--disable-error_highlight`. See [the repository]( in detail.

IRB Autocomplete and Document Display

The IRB now has an autocomplete feature, where you can just type in the code, and the completion candidates dialog will appear. You can use Tab and Shift+Tab to move up and down.

If documents are installed when you select a completion candidate, the documentation dialog will appear next to the completion candidates dialog, showing part of the content. You can read the full document by pressing Alt+d.