|
| 1 | +--- |
| 2 | +layout: news_post |
| 3 | +title: "Ruby 3.2.0 Preview 2 Released" |
| 4 | +author: "naruse" |
| 5 | +translator: "Bear Su" |
| 6 | +date: 2022-09-09 00:00:00 +0000 |
| 7 | +lang: zh_tw |
| 8 | +--- |
| 9 | + |
| 10 | +{% assign release = site.data.releases | where: "version", "3.2.0-preview2" | first %} |
| 11 | + |
| 12 | +我們很高興宣佈 Ruby {{ release.version }} 發佈了. Ruby 3.2 新增許多新功能及效能提升. |
| 13 | + |
| 14 | + |
| 15 | +## 基於 WASI 的 WebAssembly 支援 |
| 16 | + |
| 17 | +這是首次基於 WASI 支援 WebAssembly。使得 CRuby binary 可用於網頁瀏覽器、Serverless Edge 環境、與其他 WebAssembly/WASI 嵌入式環境. 目前已通過 basic 與 bootstrap 測試,但不包括 Thread API。 |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +### 背景 |
| 22 | + |
| 23 | +[WebAssembly (Wasm)](https://webassembly.org/) 最初是為了在網頁瀏覽器中安全快速地執行程式。但其目標 - 在不同的環境上安全又有效率的執行程式,不僅是 web 應用程式,也是其他一般應用程式的目標。 |
| 24 | + |
| 25 | +[WASI (The WebAssembly System Interface)](https://wasi.dev/) 被設計用於此使用場景。 儘管應用程式需要與作業系統溝通,但 WebAssembly 卻是運行在沒有系統介面的虛擬機中。WASI 將其標準化了。 |
| 26 | + |
| 27 | +Ruby 中的 WebAssembly/WASI 支援透過這些專案,允許 Ruby 開發者可以開發在相容此功能的平台上執行的應用程式。 |
| 28 | + |
| 29 | +### 使用場景 |
| 30 | + |
| 31 | +此支援功能使得開發者可以在 WebAssembly 環境上使用 CRuby。 其中一個範例就是 [TryRuby playground](https://try.ruby-lang.org/playground/) 的 CRuby 支援。現在您可以在您的網頁瀏覽器上嘗試原生的 CRuby。 |
| 32 | + |
| 33 | +### 技術特點 |
| 34 | + |
| 35 | +因為目前 WASI 和 WebAssembly 不斷地再改進與安全性理由,仍缺少一些功能來實現 Fiber、異常、和 GC。所以 CRuby 透過使用 Asyncify,一個在使用者空間的 binary 轉換技術,來彌補中間的差距。 |
| 36 | + |
| 37 | +並且,我們建置了 [a VFS on top of WASI](https://github.com/kateinoigakukun/wasi-vfs/wiki/Getting-Started-with-CRuby),讓我們可以很容易地將 Ruby 應用程式打包成單一 .wasm 檔案。簡化了 Ruby 應用程式的分發過程。 |
| 38 | + |
| 39 | + |
| 40 | +### 相關連結 |
| 41 | + |
| 42 | +* [Add WASI based WebAssembly support #5407](https://github.com/ruby/ruby/pull/5407) |
| 43 | +* [An Update on WebAssembly/WASI Support in Ruby](https://itnext.io/final-report-webassembly-wasi-support-in-ruby-4aface7d90c9) |
| 44 | + |
| 45 | +## Regexp 逾時設定 |
| 46 | + |
| 47 | +新增 Regexp matching 的逾時設定。 |
| 48 | + |
| 49 | +```ruby |
| 50 | +Regexp.timeout = 1.0 |
| 51 | + |
| 52 | +/^a*b?a*$/ =~ "a" * 50000 + "x" |
| 53 | +#=> 1 秒後拋出 Regexp::TimeoutError |
| 54 | +``` |
| 55 | + |
| 56 | +眾所皆知 Regexp matching 所花的時間可能會非預期的久。如果您的程式使用效率可能較低的 Regexp 來比對不可信的輸入內容,攻擊者可能可以藉此來發動服務阻斷攻擊。(稱為 Regular expression DoS, or ReDoS)。 |
| 57 | + |
| 58 | +根據您的 Ruby 應用程式需求,可以透過設定 `Regexp.timeout` 來避免或是減輕被 DoS 的風險。請在您的應用程式中嘗試使用,我們歡迎您的任何建議。 |
| 59 | + |
| 60 | +注意 `Regexp.timeout` 是全域設定。如果您想要為一些特定的 Regexps 使用不同的逾時設定,您可以在呼叫 `Regexp.new` 時使用 `timeout` keyword。 |
| 61 | + |
| 62 | +```ruby |
| 63 | +Regexp.timeout = 1.0 |
| 64 | + |
| 65 | +# 這個 regexp 沒有超時設定 |
| 66 | +long_time_re = Regexp.new("^a*b?a*$", timeout: nil) |
| 67 | + |
| 68 | +long_time_re =~ "a" * 50000 + "x" # 不會被中斷 |
| 69 | +``` |
| 70 | + |
| 71 | +最初提案:https://bugs.ruby-lang.org/issues/17837 |
| 72 | + |
| 73 | + |
| 74 | +## 其他值得注意的新功能 |
| 75 | + |
| 76 | +### 不再綑綁第三方原始碼 |
| 77 | + |
| 78 | +* 我們不再綑綁第三方原始碼像是 `libyaml`, `libffi`。 |
| 79 | + |
| 80 | + * psych 中的 libyaml 原始碼已經被移除。您可能需要在 Ubuntu/Debian 平台上安裝 `libyaml-dev`。 每個平台上的套件名稱有所不同。 |
| 81 | + |
| 82 | + * libffi 將在 preview2 從 `fiddle` 中移除。 |
| 83 | + |
| 84 | +### 語言功能 |
| 85 | + |
| 86 | +* 除了作為方法參數,匿名不定長度參數現在也可以傳遞為其他方法的參數。 |
| 87 | + [[Feature #18351]] |
| 88 | + |
| 89 | + ```ruby |
| 90 | + def foo(*) |
| 91 | + bar(*) |
| 92 | + end |
| 93 | + def baz(**) |
| 94 | + quux(**) |
| 95 | + end |
| 96 | + ``` |
| 97 | + |
| 98 | +* 只接收單一參數的 proc 將不會自動解開封裝。 [[Bug #18633]] |
| 99 | + |
| 100 | + ```ruby |
| 101 | + proc{|a, **k| a}.call([1, 2]) |
| 102 | + # Ruby 3.1 與之前的版本 |
| 103 | + # => 1 |
| 104 | + # Ruby 3.2 與之後的版本 |
| 105 | + # => [1, 2] |
| 106 | + ``` |
| 107 | + |
| 108 | +* 常數賦值評估順序將與單一屬性賦值評估順序保持一致。參考以下程式碼: |
| 109 | + |
| 110 | + ```ruby |
| 111 | + foo::BAR = baz |
| 112 | + ``` |
| 113 | + |
| 114 | + `foo` 現在會在 `baz` 之前被呼叫。同樣地,在有多個賦值給常數的情況,會使用從左至右的順序評估。參考以下程式碼: |
| 115 | + |
| 116 | + ```ruby |
| 117 | + foo1::BAR1, foo2::BAR2 = baz1, baz2 |
| 118 | + ``` |
| 119 | + |
| 120 | + 現在使用下面的評估顺序: |
| 121 | + |
| 122 | + 1. `foo1` |
| 123 | + 2. `foo2` |
| 124 | + 3. `baz1` |
| 125 | + 4. `baz2` |
| 126 | + |
| 127 | + [[Bug #15928]] |
| 128 | + |
| 129 | +* Find pattern 不再是實驗性功能。 |
| 130 | + [[Feature #18585]] |
| 131 | + |
| 132 | +* 使用不定長度參數 (例如 `*args`) 的方法,如果同時希望可以作為 keyword 參數傳遞給 `foo(*args)`。必須標記為 `ruby2_keywords` (若還未標記)。 |
| 133 | + 換句話說,希望作為接收 keyword 參數的其他方法都毫無例外地必須標記為 `ruby2_keywords`。若某個函式庫需要使用 Ruby 3+,這會是一個較為容易的過渡升級方法。 |
| 134 | + 在此之前,當接受方法取得 `*args`時會保留 `ruby2_keywords` 標記,但這是一個錯誤且行為不一致。 |
| 135 | + 對於找到可能缺少 `ruby2_keywords` 標記的好方法是執行測試,在測試失敗的地方,找到最後一個接收 keyword 參數的方法,在哪裡使用 `puts nil, caller, nil`,並檢查每一個在呼叫鏈上的方法/區塊,是否都被正確地標記為 `ruby2_keywords`。[[Bug #18625]] [[Bug #16466]] |
| 136 | + |
| 137 | + ```ruby |
| 138 | + def target(**kw) |
| 139 | + end |
| 140 | +
|
| 141 | + # 意外地 Ruby 2.7-3.1 在沒有 ruby2_keywords 的情況下可以成功 |
| 142 | + # 執行,但在 3.2+ 卻是必需的。若需移除 ruby2_keywords, |
| 143 | + # #foo 和 #bar 需要將參數改成 (*args, **kwargs) 或 (...) |
| 144 | + ruby2_keywords def bar(*args) |
| 145 | + target(*args) |
| 146 | + end |
| 147 | +
|
| 148 | + ruby2_keywords def foo(*args) |
| 149 | + bar(*args) |
| 150 | + end |
| 151 | +
|
| 152 | + foo(k: 1) |
| 153 | + ``` |
| 154 | + |
| 155 | +## 效能提升 |
| 156 | + |
| 157 | +### YJIT |
| 158 | + |
| 159 | +* 支援 arm64 / aarch64 架構的 UNIX 平台。 |
| 160 | +* 建置 YJIT 時需要 Rust 1.58.1+ 。 [[Feature #18481]] |
| 161 | + |
| 162 | +## 自 3.1 以來其他值得注意的變更 |
| 163 | + |
| 164 | +* Hash |
| 165 | + * 當 hash 為空時, Hash#shift 現在總是回傳 nil,取代以往回傳預設值或呼叫預設的 proc。 [[Bug #16908]] |
| 166 | + |
| 167 | +* MatchData |
| 168 | + * 已新增 MatchData#byteoffset。 [[Feature #13110]] |
| 169 | + |
| 170 | +* Module |
| 171 | + * 已新增 Module.used_refinements。 [[Feature #14332]] |
| 172 | + * 已新增 Module#refinements。 [[Feature #12737]] |
| 173 | + * 已新增 Module#const_added。 [[Feature #17881]] |
| 174 | + |
| 175 | +* Proc |
| 176 | + * Proc#dup 回傳子類別的實體。 [[Bug #17545]] |
| 177 | + * Proc#parameters 現在接受 lambda keyword。 [[Feature #15357]] |
| 178 | + |
| 179 | +* Refinement |
| 180 | + * 已新增 Refinement#refined_class。 [[Feature #12737]] |
| 181 | + |
| 182 | +* Set |
| 183 | + * Set 現在可以直接使用,不再需要先 `require "set"`。 [[Feature #16989]] |
| 184 | + 目前是透過 `Set` 常數或呼叫 `Enumerable#to_set` 來自動載入。 |
| 185 | + |
| 186 | +* String |
| 187 | + * 已新增 String#byteindex 和 String#byterindex。 [[Feature #13110]] |
| 188 | + * 更新 Unicode 至 Version 14.0.0 和 Emoji Version 14.0。 [[Feature #18037]] (也適用於 Regexp) |
| 189 | + * 已新增 String#bytesplice。 [[Feature #18598]] |
| 190 | + |
| 191 | +* Struct |
| 192 | + * `Struct.new` 不需要傳入 `keyword_init: true` 也可以透過 keyword 參數初始化。 [[Feature #16806]] |
| 193 | + |
| 194 | +## 相容性問題 |
| 195 | + |
| 196 | +注意:不包含功能問題的修正。 |
| 197 | + |
| 198 | +### 被移除的常數 |
| 199 | + |
| 200 | +下列廢棄的常數已被移除。 |
| 201 | + |
| 202 | +* `Fixnum` 和 `Bignum` [[Feature #12005]] |
| 203 | +* `Random::DEFAULT` [[Feature #17351]] |
| 204 | +* `Struct::Group` |
| 205 | +* `Struct::Passwd` |
| 206 | + |
| 207 | +### 被移除的方法 |
| 208 | + |
| 209 | +下列廢棄的方法已被移除。 |
| 210 | + |
| 211 | +* `Dir.exists?` [[Feature #17391]] |
| 212 | +* `File.exists?` [[Feature #17391]] |
| 213 | +* `Kernel#=~` [[Feature #15231]] |
| 214 | +* `Kernel#taint`, `Kernel#untaint`, `Kernel#tainted?` |
| 215 | + [[Feature #16131]] |
| 216 | +* `Kernel#trust`, `Kernel#untrust`, `Kernel#untrusted?` |
| 217 | + [[Feature #16131]] |
| 218 | + |
| 219 | +## Stdlib 相容性問題 |
| 220 | + |
| 221 | +* `Psych` 不再綑綁 libyaml 原始碼. |
| 222 | + 使用者需要透過套件管理系統自行安裝 libyaml 函式庫。 [[Feature #18571]] |
| 223 | + |
| 224 | +## C API 更新 |
| 225 | + |
| 226 | +### 被移除的 C APIs |
| 227 | + |
| 228 | +下列廢棄的 APIs 已被移除。 |
| 229 | + |
| 230 | +* `rb_cData` 變數。 |
| 231 | +* "taintedness" 和 "trustedness" 函式. [[Feature #16131]] |
| 232 | + |
| 233 | +### 標準函式庫更新 |
| 234 | + |
| 235 | +* 下列的預設 gem 已被更新。 |
| 236 | + |
| 237 | + * TBD |
| 238 | + |
| 239 | +* 下列的 bundled gem 已被更新。 |
| 240 | + |
| 241 | + * TBD |
| 242 | + |
| 243 | +* 下列的預設 gem 現在是 bundled gems。你需要在 bundler 環境下將這些函式庫加入到 `Gemfile` 中。 |
| 244 | + |
| 245 | + * TBD |
| 246 | + |
| 247 | +參見 [NEWS](https://github.com/ruby/ruby/blob/{{ release.tag }}/NEWS.md) |
| 248 | +和 [commit logs](https://github.com/ruby/ruby/compare/v3_1_0...{{ release.tag }}) 來了解更多。 |
| 249 | + |
| 250 | +自 Ruby 3.1.0 以來,計 [{{ release.stats.files_changed }} 檔案變更, {{ release.stats.insertions }} 行新增 (+), {{ release.stats.deletions }} 行刪減 (-)](https://github.com/ruby/ruby/compare/v3_1_0...{{ release.tag }}#file_bucket)! |
| 251 | + |
| 252 | +## 下載 |
| 253 | + |
| 254 | +* <{{ release.url.gz }}> |
| 255 | + |
| 256 | + SIZE: {{ release.size.gz }} |
| 257 | + SHA1: {{ release.sha1.gz }} |
| 258 | + SHA256: {{ release.sha256.gz }} |
| 259 | + SHA512: {{ release.sha512.gz }} |
| 260 | + |
| 261 | +* <{{ release.url.xz }}> |
| 262 | + |
| 263 | + SIZE: {{ release.size.xz }} |
| 264 | + SHA1: {{ release.sha1.xz }} |
| 265 | + SHA256: {{ release.sha256.xz }} |
| 266 | + SHA512: {{ release.sha512.xz }} |
| 267 | + |
| 268 | +* <{{ release.url.zip }}> |
| 269 | + |
| 270 | + SIZE: {{ release.size.zip }} |
| 271 | + SHA1: {{ release.sha1.zip }} |
| 272 | + SHA256: {{ release.sha256.zip }} |
| 273 | + SHA512: {{ release.sha512.zip }} |
| 274 | + |
| 275 | +## Ruby 是什麼 |
| 276 | + |
| 277 | +Ruby 最初由 Matz(Yukihiro Matsumoto)於 1993 年開發的開源軟體。可以在許多平台上執行。使用者來自世界各地,特別活躍於網路開發領域。 |
| 278 | + |
| 279 | + |
| 280 | + |
| 281 | +[Feature #12005]: https://bugs.ruby-lang.org/issues/12005 |
| 282 | +[Feature #12655]: https://bugs.ruby-lang.org/issues/12655 |
| 283 | +[Feature #12737]: https://bugs.ruby-lang.org/issues/12737 |
| 284 | +[Feature #13110]: https://bugs.ruby-lang.org/issues/13110 |
| 285 | +[Feature #14332]: https://bugs.ruby-lang.org/issues/14332 |
| 286 | +[Feature #15231]: https://bugs.ruby-lang.org/issues/15231 |
| 287 | +[Feature #15357]: https://bugs.ruby-lang.org/issues/15357 |
| 288 | +[Bug #15928]: https://bugs.ruby-lang.org/issues/15928 |
| 289 | +[Feature #16131]: https://bugs.ruby-lang.org/issues/16131 |
| 290 | +[Bug #16466]: https://bugs.ruby-lang.org/issues/16466 |
| 291 | +[Feature #16806]: https://bugs.ruby-lang.org/issues/16806 |
| 292 | +[Bug #16889]: https://bugs.ruby-lang.org/issues/16889 |
| 293 | +[Bug #16908]: https://bugs.ruby-lang.org/issues/16908 |
| 294 | +[Feature #16989]: https://bugs.ruby-lang.org/issues/16989 |
| 295 | +[Feature #17351]: https://bugs.ruby-lang.org/issues/17351 |
| 296 | +[Feature #17391]: https://bugs.ruby-lang.org/issues/17391 |
| 297 | +[Bug #17545]: https://bugs.ruby-lang.org/issues/17545 |
| 298 | +[Feature #17881]: https://bugs.ruby-lang.org/issues/17881 |
| 299 | +[Feature #18037]: https://bugs.ruby-lang.org/issues/18037 |
| 300 | +[Feature #18159]: https://bugs.ruby-lang.org/issues/18159 |
| 301 | +[Feature #18351]: https://bugs.ruby-lang.org/issues/18351 |
| 302 | +[Bug #18487]: https://bugs.ruby-lang.org/issues/18487 |
| 303 | +[Feature #18571]: https://bugs.ruby-lang.org/issues/18571 |
| 304 | +[Feature #18585]: https://bugs.ruby-lang.org/issues/18585 |
| 305 | +[Feature #18598]: https://bugs.ruby-lang.org/issues/18598 |
| 306 | +[Bug #18625]: https://bugs.ruby-lang.org/issues/18625 |
| 307 | +[Bug #18633]: https://bugs.ruby-lang.org/issues/18633 |
| 308 | +[Feature #18685]: https://bugs.ruby-lang.org/issues/18685 |
| 309 | +[Bug #18782]: https://bugs.ruby-lang.org/issues/18782 |
| 310 | +[Feature #18788]: https://bugs.ruby-lang.org/issues/18788 |
| 311 | +[Feature #18809]: https://bugs.ruby-lang.org/issues/18809 |
| 312 | +[Feature #18481]: https://bugs.ruby-lang.org/issues/18481 |
0 commit comments