Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rangeの2.6以降の追加対応(===, cover?, include?, to_aまわりの整理) #2529

Merged
merged 3 commits into from May 18, 2021

Conversation

universato
Copy link
Contributor

@universato universato commented Apr 15, 2021

class Range (Ruby 3.0.0 リファレンスマニュアル)
上記のページに対する===, cover?, include?まわりのメソッドの整理、to_aメソッドの追加です。

主な修正1 ===, cover?, include?まわりのメソッドの整理

self === obj -> bool
cover?(obj) -> bool

上記のように一項目で説明されている部分を、===(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?の説明が色々と混雑してくる。

その他の修正

require 'date'
p (Time.new(2014,1,3)..Time.new(2014,1,5)).include?(Time.new(2014,1,4,10,10,10)) 
#  "can't iterate from Time"
# 2.0までは上記のようなエラーがでてたが、2.1でエラーがなくなり、include?もcover?も同じような挙動となった。
  • 始端と終端が数値であれば、 [[m:Range#cover?]] と同様の動きをします。
    これが英語版の訳だったが、「cover?Rangeを引数をとるが、<=>が取らないこと」を考慮できてなかったため、微修正。

    始端・終端・引数が数値であれば、 [[m:Range#cover?]] と同様の動きをします。

主な修正2

2.6以降で、Range#to_aとして再定義されたようなので、to_aメソッドとその簡単な例を追加しました。

@universato universato changed the title Rangeno Rangeの===, cover?, include? まわりの整理 Apr 15, 2021
@universato universato changed the title Rangeの===, cover?, include? まわりの整理 Rangeの2.6以降の追加対応(===, cover?, include?, to_aまわりの整理) Apr 15, 2021
始端と終端の中に obj があるとき、true を返します。
そうでないとき、false を返します。

=== は主に case 式での比較に用いられます。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行頭の === は見出しになるので避けてください。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューありがとうございます。行頭の===がなくなるように修正しました。

@znz znz merged commit 3bbaad4 into rurema:master May 18, 2021
@universato universato deleted the range branch May 18, 2021 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants