Ruby 3.0.0 Preview 1 发布

栏目: 软件资讯 · 发布时间: 5年前

内容简介:Ruby 3.0.0 Preview 1 已经发布了,此版本引入了几个新特性。 RBS RBS 是一种描述 Ruby 程序类型的语言。类型检查器(包括类型分析器和其他支持 RBS 的工具)将通过 RBS 定义更好地理解 Ruby 程序。开发者可以写下...

Ruby 3.0.0 Preview 1 已经发布了,此版本引入了几个新特性。

RBS

RBS 是一种描述 Ruby 程序类型的语言。类型检查器(包括类型分析器和其他支持 RBS 的工具)将通过 RBS 定义更好地理解 Ruby 程序。开发者可以写下类和模块的定义:类中定义的方法、实例变量及其类型以及继承/混合关系。RBS 的目标是支持 Ruby 程序中常见的模式,它允许编写高级类型,包括联合类型、方法重载和泛型。它还支持带有接口类型的 duck typing。

Ruby 3.0 附带有rbsgem,它可以解析和处理用 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

Ractor(实验阶段)

Ractor 是类似于并发抽象的 Actor 模型,旨在提供并行执行功能而无需担心线程安全。为了限制共享对象,Ractor 对 Ruby 的语法引入了一些限制,以下程序用两个 ractor 并行计算 prime?,速度大约快 2 倍。

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

Scheduler(实验阶段)

引入Thread#scheduler用于拦截阻塞操作,允许轻量级的并发,而无需更改现有代码。

当前支持的类/方法:

  • Mutex#lockMutex#unlockMutex#sleep
  • ConditionVariable#wait
  • Queue#popSizedQueue#push
  • Thread#join
  • Kernel#sleep
  • IO#waitIO#readIO#write 与相关方法如 #wait_readable#gets#puts 
  • 不支持 IO#select 

此外还有一此新特性,详情查看更新说明:

https://www.ruby-lang.org/en/news/2020/09/25/ruby-3-0-0-preview1-released


以上所述就是小编给大家介绍的《Ruby 3.0.0 Preview 1 发布》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Computation and Programming Using Python

Introduction to Computation and Programming Using Python

John V. Guttag / The MIT Press / 2013-7 / USD 25.00

This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换