Rangeの2.6以降の追加対応(===, cover?, include?, to_aまわりの整理) #2529
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
class Range (Ruby 3.0.0 リファレンスマニュアル)
上記のページに対する
===
,cover?
,include?
まわりのメソッドの整理、to_a
メソッドの追加です。主な修正1
===
,cover?
,include?
まわりのメソッドの整理上記のように一項目で説明されている部分を、
===(obj)
とcover?(obj)
とcover?(range)
に分離させました。以下のような点からわかりにくい・誤っていると思い、分離させたかったからです。
===
とcover?
は、同じではない。===
からcover?
が呼ばれているわけでもない。cover?
にはRange
を引数にとる設計があるが、===
にはない。今のままだと、
===
もRange
を引数にとって比較できるようにしか読めない。===
は、cover?
と同じように振る舞うように2.6で変わったが、文字列は2.6で変わらなかった。以下で、backportされるべきという意見もあるが、2.6では変わってない。
https://bugs.ruby-lang.org/issues/14575
===
が case文で使われる実例が載っていない。本家の英語版には例がある。===
とcover?
の説明が色々と混雑してくる。その他の修正
cover?
とinclude?
の差異でTime
の例が載っていたが、今は同じ挙動であるため、下記で主張されていたような別の例に変更。
Feature #14575: Switch Range#=== to use cover? instead of include? Ruby Issue Tracking System
始端と終端が数値であれば、 [[m:Range#cover?]] と同様の動きをします。
これが英語版の訳だったが、「
cover?
がRange
を引数をとるが、<=>
が取らないこと」を考慮できてなかったため、微修正。↓
始端・終端・引数が数値であれば、 [[m:Range#cover?]] と同様の動きをします。
主な修正2
2.6以降で、
Range#to_a
として再定義されたようなので、to_a
メソッドとその簡単な例を追加しました。