Ruby 2.7.0 preview 2 已經發布了,最終版本計劃在 12 月發布。該版本引入了一些新特性和性能改進,主要是:
Compaction GC Pattern Matching REPL improvement Separation of positional and keyword argumentsCompaction GC
這個版本引入了 Compaction GC,以碎片化內存空間。GC.Compact 方法對堆進行壓縮,這個函數壓縮堆中的活動對象,以使用更少的頁,並且堆會更友好。
Pattern Matching(實驗性)
模式匹配是函數式程序設計語言中廣泛使用的一種特性。通過模式匹配,可以遍歷給定的對象並分配其值。
case JSON.parse('{...}', symbolize_names: true)in {name: "Alice", children: [{name: "Bob", age: age}]} p age ...end
REPL improvementirb 現在支持多行編輯,它由 reline,readline 兼容的純 Ruby 實現驅動。它還提供 rdoc 集成。在 irb 中,可以顯示給定類、模塊或方法的引用。
Separation of positional and keyword arguments關鍵字參數和位置參數的自動轉換,將在 Ruby 3 中將刪除。
其他顯著新特點: 引入編號參數作為默認塊參數 添加 Enumerable#tally["a", "b", "c", "b"].tally#=> {"a"=>1, "b"=>2, "c"=>1}
def fooendprivate :fooself.foo
添加 Enumerator::Lazy#eagera = %w(foo bar baz)e = a.lazy.map {|x| x.upcase }.map {|x| x + "!" }.eagerp e.class #=> Enumeratorp e.map {|x| x + "?" } #=> ["FOO!?", "BAR!?", "BAZ!?"]
另外,還有更新部分標準庫:
Bundler 2.1.0.pre.1 RubyGems 3.1.0.pre.1 CSV 3.1.2 (NEWS) Racc 1.4.15 REXML 3.2.3 (NEWS) RSS 0.2.8 (NEWS) StringScanner 1.0.3更多詳情見發布說明。