* [ruby-dev:52158] [Ruby Feature#14916] Proposal to add Array#===
[not found] <redmine.issue-14916.20180717170035.13476@ruby-lang.org>
@ 2025-05-16 20:54 ` matheusrich (Matheus Richard) via ruby-dev
0 siblings, 0 replies; only message in thread
From: matheusrich (Matheus Richard) via ruby-dev @ 2025-05-16 20:54 UTC (permalink / raw)
To: ruby-dev; +Cc: matheusrich (Matheus Richard)
Issue #14916 has been updated by matheusrich (Matheus Richard).
This is probably unnecessary now that pattern matching exists.
----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-113306
* Author: osyo (manga osyo)
* Status: Open
----------------------------------------
## 概要
`Array#===` を追加する提案になります。
基本的な動作は『`Array#==` の `===` で比較する版』になります。
## 仕様
配列の各要素をそれぞれ順に `===` で比較し、全要素が `true` の場合に `true` を返す。そうでない場合は `false` を返す。
### 動作例
```ruby
# 配列の各要素を #=== を使用して比較する
[ String, /\w/ ] === [ "a", "c", 7 ] # => false
[ String, /\w/, (1..10) ] === [ "a", "c", 7 ] # => true
[ String, /\w/, (1..10) ] === [ "a", "!", 42 ] # => false
```
### 真を返すケース
* レシーバと引数が配列で同じサイズかつ、各要素を `===` で比較したときに全て `true` になる場合
* レシーバと引数が同じオブジェクトの場合
### 偽を返すケース
* レシーバと引数が配列で同じサイズかつ、各要素を `===` で比較したときに `false` がある場合
* レシーバと引数の配列のサイズが異なる場合
* 引数が配列以外の場合
## ユースケース
### 引数の値によって処理を変える
可変長引数で引数を受け取り、そのまま case-when で値を精査する
```ruby
def plus *args
case args
# 数値の場合
when [Integer, Integer]
args[0] + args[1]
# 数字の場合
when [/^\d+$/, /^\d+$/]
args[0].to_i + args[1].to_i
# それ以外はエラー
else
raise "Error"
end
end
p plus 1, 2
# => 3
p plus "3", "4"
# => 7
p plus "homu", "mado"
# Error (RuntimeError)
```
### FizzBuzz を用いた例
任意の処理の結果を複数回参照したい場合、配列でまとめて case-when で利用する
```ruby
def fizzbuzz n
_ = proc { true }
case [n % 3, n % 5]
# n % 3 === 0 && n % 5 === 0
when [0, 0]
"FizzBuzz"
# n % 3 === 0
when [0, _]
"Fizz"
# n % 5 === 0
when [_, 0]
"Buzz"
else
n
end
end
p (1..20).map &method(:fizzbuzz)
# => [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz"]
```
## 関連しそうなチケット
* [Feature #14869: Proposal to add Hash#=== - Ruby trunk - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/14869?next_issue_id=14866&prev_issue_id=14870)
* [Feature #14913: Extend case to match several values at once - Ruby trunk - Ruby Issue Tracking System](https://bugs.ruby-lang.org/issues/14913)
以上、 `Array#===` に関する提案になります。
挙動に関して疑問点や意見などございましたらコメント頂けると助かります。
---Files--------------------------------
array_eqq.patch (3.06 KB)
array_eqq.patch (3.2 KB)
array_eqq.patch (3.41 KB)
--
https://bugs.ruby-lang.org/
_______________________________________________
ruby-dev mailing list -- ruby-dev@ml.ruby-lang.org
To unsubscribe send an email to ruby-dev-leave@ml.ruby-lang.org
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-05-16 20:54 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-14916.20180717170035.13476@ruby-lang.org>
2025-05-16 20:54 ` [ruby-dev:52158] [Ruby Feature#14916] Proposal to add Array#=== matheusrich (Matheus Richard) via ruby-dev
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).