Fix typos [ci skip]
[ruby-80x24.org.git] / NEWS.md
blob14f56c21919d8db6aaaca12faff7ebf182aec4a9
1 # NEWS for Ruby 3.1.0
3 This document is a list of user-visible feature changes
4 since the **3.0.0** release, except for bug fixes.
6 Note that each entry is kept to a minimum, see links for details.
8 ## Language changes
10 *   The block argument can now be anonymous if the block will
11     only be passed to another method. [[Feature #11256]]
13     ```ruby
14     def foo(&)
15       bar(&)
16     end
17     ```
19 *   Pin operator now takes an expression. [[Feature #17411]]
21     ```ruby
22     Prime.each_cons(2).lazy.find_all{_1 in [n, ^(n + 2)]}.take(3).to_a
23     #=> [[3, 5], [5, 7], [11, 13]]
24     ```
26 *   Pin operator now supports instance, class, and global variables.
27     [[Feature #17724]]
29     ```ruby
30     @n = 5
31     Prime.each_cons(2).lazy.find{_1 in [n, ^@n]}
32     #=> [3, 5]
33     ```
35 *   One-line pattern matching is no longer experimental.
37 *   Parentheses can be omitted in one-line pattern matching.
38     [[Feature #16182]]
40     ```ruby
41     [0, 1] => _, x
42     {y: 2} => y:
43     x #=> 1
44     y #=> 2
45     ```
47 *   Multiple assignment evaluation order has been made consistent with
48     single assignment evaluation order.  With single assignment, Ruby
49     uses a left-to-right evaluation order.  With this code:
51     ```ruby
52     foo[0] = bar
53     ```
55     The following evaluation order is used:
57     1. `foo`
58     2. `bar`
59     3. `[]=` called on the result of `foo`
61     In Ruby before 3.1.0, multiple assignment did not follow this
62     evaluation order.  With this code:
64     ```ruby
65     foo[0], bar.baz = a, b
66     ```
68     Versions of Ruby before 3.1.0 would evaluate in the following
69     order
71     1. `a`
72     2. `b`
73     3. `foo`
74     4. `[]=` called on the result of `foo`
75     5. `bar`
76     6. `baz=` called on the result of `bar`
78     Starting in Ruby 3.1.0, the evaluation order is now consistent with
79     single assignment, with the left-hand side being evaluated before
80     the right-hand side:
82     1. `foo`
83     2. `bar`
84     3. `a`
85     4. `b`
86     5. `[]=` called on the result of `foo`
87     6. `baz=` called on the result of `bar`
89     [[Bug #4443]]
91 *   Values in Hash literals and keyword arguments can be omitted.
92     [[Feature #14579]]
94     For example,
96     * `{x:, y:}` is a syntax sugar of `{x: x, y: y}`.
97     * `foo(x:, y:)` is a syntax sugar of `foo(x: x, y: y)`.
99     Constant names, local variable names, and method names are allowed as
100     key names.  Note that a reserved word is considered as a local
101     variable or method name even if it's a pseudo variable name such as
102     `self`.
104 *   Non main-Ractors can get instance variables (ivars) of classes/modules
105     if ivars refer to shareable objects.
106     [[Feature #17592]]
108 *   A command syntax is allowed in endless method definitions, i.e.,
109     you can now write `def foo = puts "Hello"`.
110     Note that `private def foo = puts "Hello"` does not parse.
111     [[Feature #17398]]
113 ## Command line options
115 * `--disable-gems` is now explicitly declared as "just for debugging".
116   Never use it in any real-world codebase.
117   [[Feature #17684]]
119 ## Core classes updates
121 Note: We're only listing outstanding class updates.
123 * Array
125     * Array#intersect? is added. [[Feature #15198]]
127 * Class
129     *   Class#subclasses, which returns an array of classes
130         directly inheriting from the receiver, not
131         including singleton classes.
132         [[Feature #18273]]
134         ```ruby
135         class A; end
136         class B < A; end
137         class C < B; end
138         class D < A; end
139         A.subclasses    #=> [D, B]
140         B.subclasses    #=> [C]
141         C.subclasses    #=> []
142         ```
144 * Enumerable
146     *   Enumerable#compact is added. [[Feature #17312]]
148     *   Enumerable#tally now accepts an optional hash to count. [[Feature #17744]]
150     *   Enumerable#each_cons and each_slice to return a receiver. [[GH-1509]]
152         ```ruby
153         [1, 2, 3].each_cons(2){}
154         # 3.0 => nil
155         # 3.1 => [1, 2, 3]
157         [1, 2, 3].each_slice(2){}
158         # 3.0 => nil
159         # 3.1 => [1, 2, 3]
160         ```
162 * Enumerator::Lazy
164     *   Enumerator::Lazy#compact is added. [[Feature #17312]]
166 * File
168     *   File.dirname now accepts an optional argument for the level to
169         strip path components. [[Feature #12194]]
171 * GC
173     *   "GC.measure_total_time = true" enables the measurement of GC.
174         Measurement can introduce overhead. It is enabled by default.
175         GC.measure_total_time returns the current setting.
176         GC.stat[:time] or GC.stat(:time) returns measured time
177         in milli-seconds. [[[Feature #10917]]]
179     *   GC.total_time returns measured time in nano-seconds. [[[Feature #10917]]]
181 * Integer
183     *   Integer.try_convert is added. [[Feature #15211]]
185 * Kernel
188     *   Kernel#load now accepts a module as the second argument,
189         and will load the file using the given module as the
190         top-level module. [[Feature #6210]]
192 * Marshal
194     *   Marshal.load now accepts a `freeze: true` option.
195         All returned objects are frozen except for `Class` and
196         `Module` instances. Strings are deduplicated. [[Feature #18148]]
198 * MatchData
200     *   MatchData#match is added [[Feature #18172]]
202     *   MatchData#match_length is added [[Feature #18172]]
204 * Method/UnboundMethod
206     *   Method#public?, Method#private?, Method#protected?,
207         UnboundMethod#public?, UnboundMethod#private?,
208         UnboundMethod#protected? have been added. [[Feature #11689]]
210 * Module
212     *   Module#prepend now modifies the ancestor chain if the receiver
213         already includes the argument. Module#prepend still does not
214         modify the ancestor chain if the receiver has already prepended
215         the argument. [[Bug #17423]]
217     *   Module#private, #public, #protected, and #module_function will
218         now return their arguments.  If a single argument is given, it
219         is returned. If no arguments are given, nil is returned.  If
220         multiple arguments are given, they are returned as an array.
221         [[Feature #12495]]
223 * Process
225     *   Process.\_fork is added. This is a core method for fork(2).
226         Do not call this method directly; it is called by existing
227         fork methods: Kernel.#fork, Process.fork, and IO.popen("-").
228         Application monitoring libraries can overwrite this method to
229         hook fork events. [[Feature #17795]]
231 * Struct
233     *   Passing only keyword arguments to Struct#initialize is warned.
234         You need to use a Hash literal to set a Hash to a first member.
235         [[Feature #16806]]
237     *   StructClass#keyword_init? is added [[Feature #18008]]
239 * String
241     *   Update Unicode version to 13.0.0 [[Feature #17750]]
242         and Emoji version to 13.0 [[Feature #18029]]
244     *   String#unpack and String#unpack1 now accept an `offset:` keyword
245         argument to start the unpacking after an arbitrary number of bytes
246         have been skipped. If `offset` is outside of the string bounds
247         `ArgumentError` is raised. [[Feature #18254]]
249 * Queue
251     *   Queue#initialize now accepts an Enumerable of initial values.
252         [[Feature #17327]]
254 * Thread
256     *   Thread#native_thread_id is added. [[Feature #17853]]
258 * Thread::Backtrace
260     *   Thread::Backtrace.limit, which returns the value to limit backtrace
261         length set by `--backtrace-limit` command line option, is added.
262         [[Feature #17479]]
264 * Time
266     *   Time.new now accepts optional `in:` keyword argument for the
267         timezone, as well as `Time.at` and `Time.now`, so that is now
268         you can omit minor arguments to `Time.new`. [[Feature #17485]]
270     *   Time#strftime supports RFC 3339 UTC for unknown offset local
271         time, `-0000`, as `%-z`. [[Feature #17544]]
273 * TracePoint
275     *   TracePoint.allow_reentry is added to allow reenter while TracePoint
276         callback.
277         [[Feature #15912]]
279 * $LOAD_PATH
281     *   $LOAD_PATH.resolve_feature_path does not raise. [[Feature #16043]]
283 * Fiber Scheduler
285     *   Add support for `Addrinfo.getaddrinfo` using `address_resolve` hook.
286         [[Feature #17370]]
288     *   Introduce non-blocking `Timeout.timeout` using `timeout_after` hook.
289         [[Feature #17470]]
291     *   Introduce new scheduler hooks `io_read` and `io_write` along with a
292         low level `IO::Buffer` for zero-copy read/write. [[Feature #18020]]
294     *   IO hooks `io_wait`, `io_read`, `io_write`, receive the original IO object
295         where possible. [[Bug #18003]]
297     *   Make `Monitor` fiber-safe. [[Bug #17827]]
299     *   Replace copy coroutine with pthread implementation. [[Feature #18015]]
301 * Refinement
303     *   New class which represents a module created by Module#refine.
304         `include` and `prepend` are deprecated, and `import_methods` is added
305         instead. [[Bug #17429]]
307 ## Stdlib updates
309 *   The following default gem are updated.
310     * RubyGems 3.3.3
311     * base64 0.1.1
312     * benchmark 0.2.0
313     * bigdecimal 3.1.1
314     * bundler 2.3.3
315     * cgi 0.3.1
316     * csv 3.2.2
317     * date 3.2.2
318     * did_you_mean 1.6.1
319     * digest 3.1.0
320     * drb 2.1.0
321     * erb 2.2.3
322     * error_highlight 0.3.0
323     * etc 1.3.0
324     * fcntl 1.0.1
325     * fiddle 1.1.0
326     * fileutils 1.6.0
327     * find 0.1.1
328     * io-console 0.5.9
329     * io-wait 0.2.1
330     * ipaddr 1.2.3
331     * irb 1.4.0
332     * json 2.6.1
333     * logger 1.5.0
334     * net-http 0.2.0
335     * net-protocol 0.1.2
336     * nkf 0.1.1
337     * open-uri 0.2.0
338     * openssl 3.0.0
339     * optparse 0.2.0
340     * ostruct 0.5.2
341     * pathname 0.2.0
342     * pp 0.3.0
343     * prettyprint 0.1.1
344     * psych 4.0.3
345     * racc 1.6.0
346     * rdoc 6.4.0
347     * readline 0.0.3
348     * readline-ext 0.1.4
349     * reline 0.2.8.pre.11
350     * resolv 0.2.1
351     * rinda 0.1.1
352     * ruby2_keywords 0.0.5
353     * securerandom 0.1.1
354     * set 1.0.2
355     * stringio 3.0.1
356     * strscan 3.0.1
357     * tempfile 0.1.2
358     * time 0.2.0
359     * timeout 0.2.0
360     * tmpdir 0.1.2
361     * un 0.2.0
362     * uri 0.11.0
363     * yaml 0.2.0
364     * zlib 2.1.1
365 *   The following bundled gems are updated.
366     * minitest 5.15.0
367     * power_assert 2.0.1
368     * rake 13.0.6
369     * test-unit 3.5.3
370     * rexml 3.2.5
371     * rbs 2.0.0
372     * typeprof 0.21.1
373 *   The following default gems are now bundled gems.
374     * net-ftp 0.1.3
375     * net-imap 0.2.2
376     * net-pop 0.1.1
377     * net-smtp 0.3.1
378     * matrix 0.4.2
379     * prime 0.1.2
380     * debug 1.4.0
382 * Coverage measurement now supports suspension. You can use `Coverage.suspend`
383   to stop the measurement temporarily, and `Coverage.resume` to restart it.
384   See [[Feature #18176]] in detail.
386 * Random::Formatter is moved to random/formatter.rb, so that you can
387   use `Random#hex`, `Random#base64`, and so on without SecureRandom.
388   [[Feature #18190]]
390 ## Compatibility issues
392 Note: Excluding feature bug fixes.
394 * `rb_io_wait_readable`, `rb_io_wait_writable` and `rb_wait_for_single_fd` are
395   deprecated in favour of `rb_io_maybe_wait_readable`,
396   `rb_io_maybe_wait_writable` and `rb_io_maybe_wait` respectively.
397   `rb_thread_wait_fd` and `rb_thread_fd_writable` are deprecated. [[Bug #18003]]
399 ## Stdlib compatibility issues
401 * `ERB#initialize` warns `safe_level` and later arguments even without -w.
402   [[Feature #14256]]
404 * `lib/debug.rb` is replaced with `debug.gem`
406 * `Kernel#pp` in `lib/pp.rb` uses the width of `IO#winsize` by default.
407   This means that the output width is automatically changed depending on
408   your terminal size. [[Feature #12913]]
410 * Psych 4.0 changes `Psych.load` as `safe_load` by the default.
411   You may need to use Psych 3.3.2 for migrating to this behavior.
412   [[Bug #17866]]
414 ## C API updates
416 * Documented. [[GH-4815]]
418 * `rb_gc_force_recycle` is deprecated and has been changed to a no-op.
419   [[Feature #18290]]
421 ## Implementation improvements
423 * Inline cache mechanism is introduced for reading class variables.
424   [[Feature #17763]]
426 * `instance_eval` and `instance_exec` now only allocate a singleton class when
427   required, avoiding extra objects and improving performance. [[GH-5146]]
429 * The performance of `Struct` accessors is improved. [[GH-5131]]
431 * `mandatory_only?` builtin special form to improve performance on
432   builtin methods. [[GH-5112]]
434 * Experimental feature Variable Width Allocation in the garbage collector.
435   This feature is turned off by default and can be enabled by compiling Ruby
436   with flag `USE_RVARGC=1` set. [[Feature #18045]] [[Feature #18239]]
438 ## JIT
440 * Rename Ruby 3.0's `--jit` to `--mjit`, and alias `--jit` to `--yjit`
441   on non-Windows x86-64 platforms and to `--mjit` on others.
443 ### MJIT
445 * The default `--mjit-max-cache` is changed from 100 to 10000.
447 * JIT-ed code is no longer cancelled when a TracePoint for class events
448   is enabled.
450 * The JIT compiler no longer skips compilation of methods longer than
451   1000 instructions.
453 * `--mjit-verbose` and `--mjit-warning` output "JIT cancel" when JIT-ed
454   code is disabled because TracePoint or GC.compact is used.
456 ### YJIT: New experimental in-process JIT compiler
458 New JIT compiler available as an experimental feature. [[Feature #18229]]
460 See [this blog post](https://shopify.engineering/yjit-just-in-time-compiler-cruby
461 ) introducing the project.
463 * Disabled by default, use `--yjit` command-line option to enable YJIT.
465 * Performance improvements on benchmarks based on real-world software,
466   up to 22% on railsbench, 39% on liquid-render.
468 * Fast warm-up times.
470 * Limited to Unix-like x86-64 platforms for now.
472 ## Static analysis
474 ### RBS
476 *   Generics type parameters can be bounded ([PR](https://github.com/ruby/rbs/pull/844)).
478     ```rbs
479     # `T` must be compatible with the `_Output` interface.
480     # `PrettyPrint[String]` is ok, but `PrettyPrint[Integer]` is a type error.
481     class PrettyPrint[T < _Output]
482       interface _Output
483         def <<: (String) -> void
484       end
486       attr_reader output: T
488       def initialize: (T output) -> void
489     end
490     ```
492 *   Type aliases can be generic. ([PR](https://github.com/ruby/rbs/pull/823))
494     ```rbs
495     # Defines a generic type `list`.
496     type list[T] = [ T, list[T] ]
497                  | nil
499     type str_list = list[String]
500     type int_list = list[Integer]
501     ```
503 * [rbs collection](https://github.com/ruby/rbs/blob/master/docs/collection.md) has been introduced to manage gems’ RBSs.
505 * Many signatures for built-in and standard libraries have been added/updated.
507 * It includes many bug fixes and performance improvements too.
509 See the [CHANGELOG.md](https://github.com/ruby/rbs/blob/master/CHANGELOG.md) for more information.
511 ### TypeProf
513 * [Experimental IDE support](https://github.com/ruby/typeprof/blob/master/doc/ide.md) has been implemented.
514 * Many bug fixes and performance improvements since Ruby 3.0.0.
516 ## Debugger
518 * A new debugger [debug.gem](https://github.com/ruby/debug) is bundled.
519   debug.gem is a fast debugger implementation, and it provides many features
520   like remote debugging, colorful REPL, IDE (VSCode) integration, and more.
521   It replaces `lib/debug.rb` standard library.
523 * `rdbg` command is also installed into `bin/` directory to start and control
524   debugging execution.
526 ## error_highlight
528 A built-in gem called error_highlight has been introduced.
529 It shows fine-grained error locations in the backtrace.
531 Example: `title = json[:article][:title]`
533 If `json` is nil, it shows:
536 $ ruby test.rb
537 test.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
539 title = json[:article][:title]
540             ^^^^^^^^^^
543 If `json[:article]` returns nil, it shows:
546 $ ruby test.rb
547 test.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
549 title = json[:article][:title]
550                       ^^^^^^^^
553 This feature is enabled by default.
554 You can disable it by using a command-line option `--disable-error_highlight`.
555 See [the repository](https://github.com/ruby/error_highlight) in detail.
557 ## Miscellaneous changes
559 * lib/objspace/trace.rb is added, which is a tool for tracing the object
560   allocation. Just by requiring this file, tracing is started *immediately*.
561   Just by `Kernel#p`, you can investigate where an object was created.
562   Note that just requiring this file brings a large performance overhead.
563   This is only for debugging purposes. Do not use this in production.
564   [[Feature #17762]]
566 * Now exceptions raised in finalizers will be printed to `STDERR`, unless
567   `$VERBOSE` is `nil`.  [[Feature #17798]]
569 * `ruby -run -e httpd` displays URLs to access.  [[Feature #17847]]
571 * Add `ruby -run -e colorize` to colorize Ruby code using
572   `IRB::Color.colorize_code`.
574 [Bug #4443]:      https://bugs.ruby-lang.org/issues/4443
575 [Feature #6210]:  https://bugs.ruby-lang.org/issues/6210
576 [Feature #10917]: https://bugs.ruby-lang.org/issues/10917
577 [Feature #11256]: https://bugs.ruby-lang.org/issues/11256
578 [Feature #11689]: https://bugs.ruby-lang.org/issues/11689
579 [Feature #12194]: https://bugs.ruby-lang.org/issues/12194
580 [Feature #12495]: https://bugs.ruby-lang.org/issues/12495
581 [Feature #12913]: https://bugs.ruby-lang.org/issues/12913
582 [Feature #14256]: https://bugs.ruby-lang.org/issues/14256
583 [Feature #14579]: https://bugs.ruby-lang.org/issues/14579
584 [Feature #15198]: https://bugs.ruby-lang.org/issues/15198
585 [Feature #15211]: https://bugs.ruby-lang.org/issues/15211
586 [Feature #15912]: https://bugs.ruby-lang.org/issues/15912
587 [Feature #16043]: https://bugs.ruby-lang.org/issues/16043
588 [Feature #16182]: https://bugs.ruby-lang.org/issues/16182
589 [Feature #16806]: https://bugs.ruby-lang.org/issues/16806
590 [Feature #17312]: https://bugs.ruby-lang.org/issues/17312
591 [Feature #17327]: https://bugs.ruby-lang.org/issues/17327
592 [Feature #17370]: https://bugs.ruby-lang.org/issues/17370
593 [Feature #17398]: https://bugs.ruby-lang.org/issues/17398
594 [Feature #17411]: https://bugs.ruby-lang.org/issues/17411
595 [Bug #17423]:     https://bugs.ruby-lang.org/issues/17423
596 [Bug #17429]:     https://bugs.ruby-lang.org/issues/17429
597 [Feature #17470]: https://bugs.ruby-lang.org/issues/17470
598 [Feature #17479]: https://bugs.ruby-lang.org/issues/17479
599 [Feature #17485]: https://bugs.ruby-lang.org/issues/17485
600 [Feature #17544]: https://bugs.ruby-lang.org/issues/17544
601 [Feature #17592]: https://bugs.ruby-lang.org/issues/17592
602 [Feature #17684]: https://bugs.ruby-lang.org/issues/17684
603 [Feature #17724]: https://bugs.ruby-lang.org/issues/17724
604 [Feature #17744]: https://bugs.ruby-lang.org/issues/17744
605 [Feature #17750]: https://bugs.ruby-lang.org/issues/17750
606 [Feature #17762]: https://bugs.ruby-lang.org/issues/17762
607 [Feature #17763]: https://bugs.ruby-lang.org/issues/17763
608 [Feature #17795]: https://bugs.ruby-lang.org/issues/17795
609 [Feature #17798]: https://bugs.ruby-lang.org/issues/17798
610 [Bug #17827]:     https://bugs.ruby-lang.org/issues/17827
611 [Feature #17847]: https://bugs.ruby-lang.org/issues/17847
612 [Feature #17853]: https://bugs.ruby-lang.org/issues/17853
613 [Bug #17866]:     https://bugs.ruby-lang.org/issues/17866
614 [Bug #18003]:     https://bugs.ruby-lang.org/issues/18003
615 [Feature #18008]: https://bugs.ruby-lang.org/issues/18008
616 [Feature #18015]: https://bugs.ruby-lang.org/issues/18015
617 [Feature #18020]: https://bugs.ruby-lang.org/issues/18020
618 [Feature #18029]: https://bugs.ruby-lang.org/issues/18029
619 [Feature #18045]: https://bugs.ruby-lang.org/issues/18045
620 [Feature #18148]: https://bugs.ruby-lang.org/issues/18148
621 [Feature #18172]: https://bugs.ruby-lang.org/issues/18172
622 [Feature #18176]: https://bugs.ruby-lang.org/issues/18176
623 [Feature #18190]: https://bugs.ruby-lang.org/issues/18190
624 [Feature #18229]: https://bugs.ruby-lang.org/issues/18229
625 [Feature #18239]: https://bugs.ruby-lang.org/issues/18239
626 [Feature #18254]: https://bugs.ruby-lang.org/issues/18254
627 [Feature #18273]: https://bugs.ruby-lang.org/issues/18273
628 [Feature #18290]: https://bugs.ruby-lang.org/issues/18290
630 [GH-1509]: https://github.com/ruby/ruby/pull/1509
631 [GH-4815]: https://github.com/ruby/ruby/pull/4815
632 [GH-5112]: https://github.com/ruby/ruby/pull/5112
633 [GH-5131]: https://github.com/ruby/ruby/pull/5131
634 [GH-5146]: https://github.com/ruby/ruby/pull/5146