ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:50974] Ruby 3.0.0 Preview 2 Released
@ 2020-12-08 10:14 NARUSE, Yui
  0 siblings, 0 replies; only message in thread
From: NARUSE, Yui @ 2020-12-08 10:14 UTC (permalink / raw)
  To: Ruby developers, ruby developers list

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
  class Channel
    attr_reader name: String
    attr_reader messages: Array[Message]
    attr_reader users: Array[User | Bot]              # `|` means
union types, `User` or `Bot`.
    def initialize: (String) -> void
    def post: (String, from: User | Bot) -> Message   # Method
overloading is supported.
            | (File, from: User | Bot) -> Message
  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
  def initialize(name:, age:)
    @name, @age = name, age
  end
  attr_reader :name, :age
end
User.new(name: "John", age: 20)
```
An example output:
```
$ typeprof test.rb
# Classes
class User
  attr_reader name : String
  attr_reader age : Integer
  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
r1, r2 = *(1..2).map do
  Ractor.new do
    n = Ractor.recv
    n.prime?
  end
end
# send parameters
r1.send 2**61 - 1
r2.send 2**61 + 15
# wait for the results of expr1, expr2
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`
- `Thread#join`
- `Kernel#sleep`
- `Process.wait`
- `IO#wait`, `IO#read`, `IO#write` and related methods (e.g.
`#wait_readable`, `#gets`, `#puts` and so on).
- `IO#select` is *not supported*.
(Explain Async gem with links). This example program will perform
several HTTP requests concurrently:
(Explain this:)
1. async is outer gem
2. async uses this new feature
``` ruby
require 'async'
require 'net/http'
require 'uri'
Async do
  ["ruby", "python", "c"].each do |topic|
    Async do
      Net::HTTP.get(URI "https://www.google.com/search?q=#{topic}")
    end
  end
end
```
## Other Notable New Features
* One-line pattern matching now uses `=>` instead of `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
    ```
* 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
    ```
* Endless method definition is added.
  ```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
  ```
* `Hash#except` is now built-in.
  ```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.
    ```ruby
    def method_missing(meth, ...)
      send(:"do_#{ meth }", ...)
    end
    ```
* The `$SAFE` feature was completely removed; now it is a normal
global variable.
* The order of backtrace had been reversed at Ruby 2.5, and is
reverted.  Now it behaves like Ruby 2.4; an error message and the line
number where the exception occurs are printed first, and its callers
are printed later.
* Some standard libraries are updated.
  * RubyGems 3.2.0.rc.1
  * Bundler 2.2.0.rc.1
  * IRB 1.2.6
  * Reline 0.1.5
* The following libraries are no longer bundled gems.
  Install the corresponding gems to use these features.
  * net-telnet
  * xmlrpc
* The following default gems are now bundled gems.
  * rexml
  * rss
* The following stdlib files are now default gems and are published on
rubygems.org.
  * abbrev
  * base64
  * English
  * erb
  * find
  * io-nonblock
  * io-wait
  * net-ftp
  * net-http
  * net-imap
  * net-protocol
  * nkf
  * open-uri
  * optparse
  * resolv
  * resolv-replace
  * rinda
  * securerandom
  * set
  * shellwords
  * tempfile
  * time
  * 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.
With those changes, [3776 files changed, 181573 insertions(+), 145096
deletions(-)](https://github.com/ruby/ruby/compare/v2_7_0...v3_0_0_preview2)
since Ruby 2.7.0!
Please try Ruby 3.0.0-preview2, and give us any feedback!
## Download
* <https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.0-preview2.tar.gz>
      SIZE: 19378626
      SHA1: 25363b20225850224e7835e99906c52f2ff57792
      SHA256: 9de8661565c2b1007d91a580e9a7e02d23f1e8fc8df371feb15a2727aa05fd9a
      SHA512: 6fa4191425ae71e41894b60bd9c31d483a562ee8216886360ce18238ab48115b95be0367708612c45f634e7584fba8940a524ba0113ce0f36ce4df78a112d0b7
* <https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.0-preview2.tar.xz>
      SIZE: 14244252
      SHA1: 54e4d3892ce480106382bd2d36dd7395e01b0f2a
      SHA256: 03078e82d4fb55c13837c69e56565fc49c451d11b1ca5e1b075d990d0957f181
      SHA512: 8b0e6e3ba7e5f95586b4438d965e7b09187ad599f4ac22dec3db7b176358514fe0c0890dde8912fef1ef92ffcde3f6f1228178eabadcf3a05601e5b6f05881ae
* <https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.0-preview2.zip>
      SIZE: 23907144
      SHA1: 064ee265c94b3df87e737622ba84437ea0d6aeaf
      SHA256: 19e295ae50934ddac2b366f0c7c8de9bd710d596b76eba02152f3641e5ce2b23
      SHA512: 598def50ef9e8ae1f44e05ff2c4e35acf252437286f08644ba5e301ebff2db399140bafa72868877100d6ffa736a4474cb7b99ecea8bdf835ed113ab250bb3d9
## 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.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-08 10:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-08 10:14 [ruby-dev:50974] Ruby 3.0.0 Preview 2 Released NARUSE, Yui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).