From e6f483203e0aea5280d62f7c4d372394dccf70dd Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Tue, 8 Dec 2020 20:00:54 +0900 Subject: [PATCH] adjust markdown format for jekyll --- ...2020-12-08-ruby-3-0-0-preview2-released.md | 87 +++++++++++--- ...2020-12-08-ruby-3-0-0-preview2-released.md | 112 +++++++++++++----- 2 files changed, 152 insertions(+), 47 deletions(-) diff --git a/en/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md b/en/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md index 60161fcfb7..e9f1a21937 100644 --- a/en/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md +++ b/en/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md @@ -6,16 +6,26 @@ translator: date: 2020-12-08 00:00:00 +0000 lang: en --- + We are pleased to announce the release of Ruby 3.0.0-preview2. + It introduces a number of new features and performance improvements. + ## Static Analysis + ### RBS + RBS is a language to describe the types of Ruby programs. + Type checkers including TypeProf and other tools supporting RBS will understand Ruby programs much better with RBS definitions. + You can write down the definition of classes and modules: methods defined in the class, instance variables and their types, and inheritance/mix-in relations. + The goal of RBS is to support commonly seen patterns in Ruby programs and it allows writing advanced types including union types, method overloading, and generics. It also supports duck typing with _interface types_. + Ruby 3.0 ships with `rbs` gem, which allows parsing and processing type definitions written in RBS. The following is a small example of RBS with class, module, and constant definitions. + ``` rbs module ChatApp VERSION: String @@ -29,13 +39,21 @@ module ChatApp end end ``` + See [README of rbs gem](https://github.com/ruby/rbs) for more detail. + ### TypeProf + TypeProf is a type analysis tool bundled in the Ruby package. + Currently, TypeProf serves as a kind of type inference. + It reads plain (non-type-annotated) Ruby code, analyzes what methods are defined and how they are used, and generates a prototype of type signature in RBS format. + Here is a simple demo of TypeProf. + An example input: + ``` ruby # test.rb class User @@ -46,7 +64,9 @@ class User end User.new(name: "John", age: 20) ``` + An example output: + ``` $ typeprof test.rb # Classes @@ -56,16 +76,26 @@ class User def initialize : (name: String, age: Integer) -> [String, Integer] end ``` + You can run TypeProf by saving the input as "test.rb" and invoke a command called "typeprof test.rb". + You can also [try TypeProf online](https://mame.github.io/typeprof-playground/#rb=%23+test.rb%0Aclass+User%0A++def+initialize%28name%3A%2C+age%3A%29%0A++++%40name%2C+%40age+%3D+name%2C+age%0A++end%0A++%0A++attr_reader+%3Aname%2C+%3Aage%0Aend%0A%0AUser.new%28name%3A+%22John%22%2C+age%3A+20%29&rbs=). (It runs TypeProf on the server side, so sorry if it is out!) + See [the documentation](https://github.com/ruby/typeprof/blob/master/doc/doc.md) and [demos](https://github.com/ruby/typeprof/blob/master/doc/demo.md) for details. + TypeProf is experimental and not so mature yet; only a subset of the Ruby language is supported, and the detection of type errors is limited. But it is still growing rapidly to improve the coverage of language features, the analysis performance, and usability. Any feedback is very welcome. + ## Ractor (experimental) Ractor is an Actor-model like concurrent abstraction designed to provide a parallel execution feature without thread-safety concerns. + You can make multiple ractors and you can run them in parallel. Ractor enables you to make thread-safe parallel programs because ractors can not share normal objects. Communication between ractors are supported by message passing. + To limit sharing of objects, Ractor introduces several restrictions to the Ruby's syntax (without multiple Ractors, there is no restriction). + The specification and implmentation are not matured and may be changed in the future, so this feature is marked as experimental and show the "experimental feature" warning when the first `Ractor.new`. + The following small program calculates `n.prime?` (`n` is relatively a big integer) in parallel with two ractors. You will confirm that the program execution is about x2 times faster ompare with the sequential program on the parallel computer. + ``` ruby require 'prime' # n.prime? with sent integers in r1, r2 run in parallel @@ -82,10 +112,15 @@ r2.send 2**61 + 15 p r1.take #=> true p r2.take #=> true ``` + See [doc/ractor.md](https://github.com/ruby/ruby/blob/master/doc/ractor.md) for more details. + ## Fiber Scheduler + `Fiber#scheduler` is introduced for intercepting blocking operations. This allows for light-weight concurrency without changing existing code. Watch ["Don't Wait For Me, Scalable Concurrency for Ruby 3"](https://www.youtube.com/watch?v=Y29SSOS4UOc) for an overview of how it works. + Currently supported classes/methods: + - `Mutex#lock`, `Mutex#unlock`, `Mutex#sleep` - `ConditionVariable#wait` - `Queue#pop`, `SizedQueue#push` @@ -98,6 +133,7 @@ Currently supported classes/methods: (Explain this:) 1. async is outer gem 2. async uses this new feature + ``` ruby require 'async' require 'net/http' @@ -110,7 +146,9 @@ Async do end end ``` + ## Other Notable New Features + * One-line pattern matching now uses `=>` instead of `in`. ``` ruby # version 3.0 @@ -131,30 +169,34 @@ end end ``` * Endless method definition is added. - ``` ruby - def square(x) = x * x - ``` + ``` ruby + def square(x) = x * x + ``` * Find pattern is added. - ``` ruby - case ["a", 1, "b", "c", 2, "d", "e", "f", 3] - in [*pre, String => x, String => y, *post] - p pre #=> ["a", 1] - p x #=> "b" - p y #=> "c" - p post #=> [2, "d", "e", "f", 3] - end - ``` + ``` ruby + case ["a", 1, "b", "c", 2, "d", "e", "f", 3] + in [*pre, String => x, String => y, *post] + p pre #=> ["a", 1] + p x #=> "b" + p y #=> "c" + p post #=> [2, "d", "e", "f", 3] + end + ``` * `Hash#except` is now built-in. - ``` ruby - h = { a: 1, b: 2, c: 3 } - p h.except(:a) #=> {:b=>2, :c=>3} - ``` + ``` ruby + h = { a: 1, b: 2, c: 3 } + p h.except(:a) #=> {:b=>2, :c=>3} + ``` * Memory view is added as an experimental feature * This is a new C-API set to exchange a raw memory area, such as a numeric array and a bitmap image, between extension libraries. The extension libraries can share also the metadata of the memory area that consists of the shape, the element format, and so on. Using these kinds of metadata, the extension libraries can share even a multidimensional array appropriately. This feature is designed by referring to Python's buffer protocol. + ## Performance improvements + * Many improvements were implemented in MJIT. See NEWS in detail. * Pasting long code to IRB is 53 times faster than bundled with Ruby 2.7.0. For example, the time required to paste [this sample code](https://gist.github.com/aycabta/30ab96334275bced5796f118c9220b0b) goes from 11.7 seconds to 0.22 seconds. + ## Other notable changes since 2.7 + * Keyword arguments are separated from other arguments. * In principle, code that prints a warning on Ruby 2.7 won't work. See the [document](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/) in detail. * By the way, arguments forwarding now supports leading arguments. @@ -203,30 +245,43 @@ end * tmpdir * tsort * weakref + See [NEWS](https://github.com/ruby/ruby/blob/v3_0_0_preview2/NEWS.md) or [commit logs](https://github.com/ruby/ruby/compare/v2_7_0...v3_0_0_preview2) for more details. + {% assign release = site.data.releases | where: "version", "3.0.0-preview2" | first %} + With those changes, [{{ release.stats.files_changed }} files changed, {{ release.stats.insertions }} insertions(+), {{ release.stats.deletions }} deletions(-)](https://github.com/ruby/ruby/compare/v2_7_0...v3_0_0) since Ruby 2.7.0! + Please try Ruby 3.0.0-preview2, and give us any feedback! + ## Download + * <{{ release.url.gz }}> + SIZE: {{ release.size.gz }} SHA1: {{ release.sha1.gz }} SHA256: {{ release.sha256.gz }} SHA512: {{ release.sha512.gz }} + * <{{ release.url.xz }}> + SIZE: {{ release.size.xz }} SHA1: {{ release.sha1.xz }} SHA256: {{ release.sha256.xz }} SHA512: {{ release.sha512.xz }} + * <{{ release.url.zip }}> + SIZE: {{ release.size.zip }} SHA1: {{ release.sha1.zip }} SHA256: {{ release.sha256.zip }} SHA512: {{ release.sha512.zip }} + ## What is Ruby + Ruby was first developed by Matz (Yukihiro Matsumoto) in 1993, and is now developed as Open Source. It runs on multiple platforms and is used all over the world especially for web development. diff --git a/ja/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md b/ja/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md index c6b2107c19..39ec4ceffd 100644 --- a/ja/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md +++ b/ja/news/_posts/2020-12-08-ruby-3-0-0-preview2-released.md @@ -10,12 +10,19 @@ Ruby 3.0に向けてフィードバックを得るためのリリースである Ruby 3.0には、多くの新しい機能やパフォーマンスの改善が含まれます。 その一部を以下に紹介します。 ## 静的解析 ### RBS + RBSはRubyプログラムの型を記述するための言語です。 + TypeProfなどの型検査ツールを初めとする静的解析を行うツールは、RBSを利用することでRubyプログラムをより精度良く解析することができます。 + RBSでは、Rubyプログラムのクラスやモジュールの型を定義します。メソッドやインスタンス変数、定数とその型、継承やmixinなどの関係などが記述できます。 + RBSはRubyプログラムに頻出するパターンをサポートするように設計されており、ユニオン型、メソッドオーバーロード、ジェネリクスなどの機能を提供します。さらに「インタフェース型」によってダックタイピングをサポートします。 + Ruby 3.0には、このRBS言語で書かれた型定義を処理するためのライブラリである `rbs` gemが同梱されています。 + クラスやモジュール、定数を定義する、簡単なRBSの例を示します。 + ``` rbs module ChatApp VERSION: String @@ -29,13 +36,21 @@ module ChatApp end end ``` + 詳細については、[rbs gemのREADME](https://github.com/ruby/rbs)を参照してください。 + ### TypeProf + TypeProf は Ruby パッケージに同梱された型解析ツールです。 + TypeProf の現在の主な用途は一種の型推論です。 + 型注釈の無い普通の Ruby コードを入力し、どんなメソッドが定義されどのように使われているかを解析し、型シグネチャのプロトタイプを RBS フォーマットで生成します。 + 次は TypeProf の簡単なデモです。 + サンプル入力 + ``` ruby # test.rb class User @@ -46,7 +61,9 @@ class User end User.new(name: "John", age: 20) ``` + サンプル出力 + ``` $ typeprof test.rb # Classes @@ -56,16 +73,27 @@ class User def initialize : (name: String, age: Integer) -> [String, Integer] end ``` + サンプル入力を"test.rb"という名前で保存し、`typeprof test.rb`というコマンドで TypeProf の解析ができます。 + [TypeProf をオンラインで試す](https://mame.github.io/typeprof-playground/#rb=%23+test.rb%0Aclass+User%0A++def+initialize%28name%3A%2C+age%3A%29%0A++++%40name%2C+%40age+%3D+name%2C+age%0A++end%0A++%0A++attr_reader+%3Aname%2C+%3Aage%0Aend%0A%0AUser.new%28name%3A+%22John%22%2C+age%3A+20%29&rbs=)こともできます(サーバサイドで TypeProf を動かしているので、サーバが落ちたらごめんなさい)。 + 詳しくは[ドキュメント](https://github.com/ruby/typeprof/blob/master/doc/doc.md)や[デモ](https://github.com/ruby/typeprof/blob/master/doc/demo.md)を見てください。 + 残念ながら TypeProf はまだ実験的で、あまり完成度は高くありません。Ruby 言語のサブセットだけがサポートされていて、型エラー検出の機能は限定的です。ですがいま急速に改良中であり、言語機能のカバレッジ増強、解析効率の向上、利便性の向上などを行っています。フィードバックを歓迎します。 + ## Ractor (experimental) + Ractor はアクターモデル風の並行・並列制御機構であり、スレッド安全に関する懸念をなく、Rubyで並列処理を行うための機能として設計されています。 + 複数のRactorを作成すると、それらは並列計算機上で並列に実行されます。Ractor間では、ほとんどのオブジェクトが共有できないように設計されているため、スレッド安全なプログラムにすることができます。メッセージの送受信により、Ractor間のコミュニケーションを行うことが可能です。 + Ractor間でのオブジェクトの共有を制限するために、複数Ractorでの実行時には、いくつかのRubyの機能に制限が入ります(ただし、複数のRactorを用いない場合には、これまでのRubyと何も変わりません)。 + Ractorの仕様と実装は、まだ発展途上であるため、実験的機能として提供されます。初回のRactorの生成時には実験的機能であることが警告で表示されます。 + 次の小さなプログラムでは、二つのRactorを用いて`n.prime?`(`n`は比較的大きな値)の計算を並列に実行します。動かしてみると、逐次実行にくらべて、2コア以上の計算機で計算時間が半分程度になることが確認できます。 + ``` ruby require 'prime' # n.prime? with sent integers in r1, r2 run in parallel @@ -82,10 +110,14 @@ r2.send 2**61 + 15 p r1.take #=> true p r2.take #=> true ``` + より詳細は、[doc/ractor.md](https://github.com/ruby/ruby/blob/master/doc/ractor.md) をご覧ください。 + ## Scheduler (experimental) + `Thread#scheduler` is introduced for intercepting blocking operations. This allows for light-weight concurrency without changing existing code. Watch ["Don't Wait For Me, Scalable Concurrency for Ruby 3"](https://www.youtube.com/watch?v=Y29SSOS4UOc) for an overview of how it works. Currently supported classes/methods: + - `Mutex#lock`, `Mutex#unlock`, `Mutex#sleep` - `ConditionVariable#wait` - `Queue#pop`, `SizedQueue#push` @@ -93,49 +125,56 @@ Currently supported classes/methods: - `Kernel#sleep` - `IO#wait`, `IO#read`, `IO#write` and related methods (e.g. `#wait_readable`, `#gets`, `#puts` and so on). - `IO#select` is *not supported*. + The current entry point for concurrency is `Fiber.schedule{...}` however this is subject to change by the time Ruby 3 is released. + Currently, there is a test scheduler available in [`Async::Scheduler`](https://github.com/socketry/async/pull/56). See [`doc/scheduler.md`](https://github.com/ruby/ruby/blob/master/doc/scheduler.md) for more details. + ## その他の主要な新機能 + * 1行パターンマッチが `in` の代わりに `=>` を使うようになりました。 -``` ruby - # version 3.0 - {a: 0, b: 1} => {a:} - p a # => 0 - # version 2.7 - {a: 0, b: 1} in {a:} - p a # => 0 -``` + ``` ruby + # version 3.0 + {a: 0, b: 1} => {a:} + p a # => 0 + # version 2.7 + {a: 0, b: 1} in {a:} + p a # => 0 + ``` * 一行メソッド定義が書けるようになりました。 -``` ruby - def square(x) = x * x -``` + ``` ruby + def square(x) = x * x + ``` * findパターンが書けるようになりました。 -``` ruby - case ["a", 1, "b", "c", 2, "d", "e", "f", 3] - in [*pre, String => x, String => y, *post] - p pre #=> ["a", 1] - p x #=> "b" - p y #=> "c" - p post #=> [2, "d", "e", "f", 3] - end -``` + ``` ruby + case ["a", 1, "b", "c", 2, "d", "e", "f", 3] + in [*pre, String => x, String => y, *post] + p pre #=> ["a", 1] + p x #=> "b" + p y #=> "c" + p post #=> [2, "d", "e", "f", 3] + end + ``` * `Hash#except` が組み込みになりました。 -``` ruby - h = { a: 1, b: 2, c: 3 } - p h.except(:a) #=> {:b=>2, :c=>3} -``` + ``` ruby + h = { a: 1, b: 2, c: 3 } + p h.except(:a) #=> {:b=>2, :c=>3} + ``` ## パフォーマンスの改善 + * MJITに多数の改善が行われています。詳細はNEWSを参照してください。 * IRB への長いコードの貼り付けは、Ruby 2.7.0 にバンドルされているものと比較して 53 倍の速さになります。例えば、[このサンプルコード](https://gist.github.com/aycabta/30ab96334275bced5796f118c9220b0b)の貼り付けに要する時間は、11.7 秒から 0.22 秒になります。 + ## その他の注目すべき 2.7 からの変更点 + * キーワード引数が通常の引数から分離されました。 * 原則として、2.7 で警告の出ていたコードは動かなくなります。詳細は[別ドキュメント](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/)を参照してください。 * 関連して、引数のフォワーディングの記法で先頭に引数を書けるようになりました。 -``` ruby - def method_missing(meth, ...) - send(:"do_#{ meth }", ...) - end -``` + ``` ruby + def method_missing(meth, ...) + send(:"do_#{ meth }", ...) + end + ``` * `$SAFE` の機能が完全に削除され、ただのグローバル変数となりました。 * バックトレースの順序は2.5で逆転しましたが、3.0ではこれを取りやめることになりました。例外が起きた行が先に表示され、呼び出し元が後に表示されるように戻ります。 * いくつかの標準ライブラリがアップデートされました。 @@ -175,27 +214,38 @@ Currently, there is a test scheduler available in [`Async::Scheduler`](https://g * tmpdir * tsort * weakref + その他詳細については、[NEWS](https://github.com/ruby/ruby/blob/v3_0_0_preview2/NEWS) ファイルまたは[コミットログ](https://github.com/ruby/ruby/compare/v2_7_0...v3_0_0_preview2)を参照してください。 + {% assign release = site.data.releases | where: "version", "3.0.0-preview2" | first %} + なお、こうした変更により、Ruby 2.7.0 以降では [{{ release.stats.files_changed }} 個のファイルに変更が加えられ、{{ release.stats.insertions }} 行の追加と {{ release.stats.deletions }} 行の削除が行われました](https://github.com/ruby/ruby/compare/v2_7_0...v3_0_0) ! + ぜひ Ruby 3.0.0-preview2 を試して、フィードバックをお願いします! + ## ダウンロード + * <{{ release.url.gz }}> + SIZE: {{ release.size.gz }} SHA1: {{ release.sha1.gz }} SHA256: {{ release.sha256.gz }} SHA512: {{ release.sha512.gz }} + * <{{ release.url.xz }}> + SIZE: {{ release.size.xz }} SHA1: {{ release.sha1.xz }} SHA256: {{ release.sha256.xz }} SHA512: {{ release.sha512.xz }} + * <{{ release.url.zip }}> + SIZE: {{ release.size.zip }} SHA1: {{ release.sha1.zip }} SHA256: {{ release.sha256.zip }} SHA512: {{ release.sha512.zip }} -## 3.0.0-preview2 予告 -静的型解析である["type-profiler"](https://github.com/mame/ruby-type-profiler)が入る予定です。お楽しみに。 + ## Ruby とは + Rubyはまつもとゆきひろ (Matz) によって1993年に開発が始められ、今もオープンソースソフトウェアとして開発が続けられています。Rubyは様々なプラットフォームで動き、世界中で、特にWebアプリケーション開発のために使われています。