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

Add Array#minmax since Ruby 2.7 #2260

Merged
merged 1 commit into from
Jun 2, 2020

Conversation

pocke
Copy link
Member

@pocke pocke commented May 22, 2020

#2071

Ruby 2.7から追加された Array#minmax のドキュメントを追加します。

RDoc: https://docs.ruby-lang.org/en/master/Array.html#method-i-minmax

a.minmax{|a,b| a.length <=> b.length } #=> ["dog", "albatross"]
[].minmax # => [nil, nil]
#@samplecode
a = %w(albatross dog horse)
Copy link
Member Author

Choose a reason for hiding this comment

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

Enumerable#minmax のサンプルコードで(Ruby 2.7以降は)実は Enumerable#minmax が呼ばれていない、というのはちょっとモヤっとしましたが、Enumerable#min でも同様だし、わかりやすさを考えるとまあ良いかなと言う気がしました。

自身の各要素のうち最小の要素と最大の要素を
要素とするサイズ 2 の配列を返します。

一つ目の形式は、各要素がすべて <=> メソッドを実装していることを仮定し
Copy link
Member Author

Choose a reason for hiding this comment

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

Enumerable#minmax では「すべての要素が Comparable を実装していることを仮定しています」と書かれていますが、Array#minmaxのRDocを読むとComparableには触れていなくて<=>を使うとしか書かれていないので、そのように書きました。

おそらくEnumerableの方もComparableはいらなくて<=>だけで良いのではないかと想像しています。
自信がないので ruby-jp のslackで質問中です。
<=>だけで良いのが正しければ、RDocとるりま両方を修正しようかなと思います。

@znz znz merged commit e4b849f into rurema:master Jun 2, 2020
@pocke pocke deleted the Array#minmax-since-ruby-2.7 branch June 2, 2020 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants