From: manga.osyo@gmail•com
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50593] [Ruby trunk Feature#14916] Proposal to add Array#===
Date: Tue, 17 Jul 2018 17:00:36 +0000 (UTC) [thread overview]
Message-ID: <redmine.issue-14916.20180717170035.158c115d0306d10f@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14916.20180717170035@ruby-lang.org
Issue #14916 has been reported by osyo (manga osyo).
----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916
* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
## 概要
`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)
--
https://bugs.ruby-lang.org/
next parent reply other threads:[~2018-07-17 17:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
2018-07-17 17:00 ` manga.osyo [this message]
2018-08-04 16:22 ` [ruby-dev:50602] [Ruby trunk Feature#14916] Proposal to add Array#=== keystonelemur
2018-08-06 14:32 ` [ruby-dev:50608] " eregontp
2018-08-08 13:21 ` [ruby-dev:50612] " manga.osyo
2018-08-08 20:46 ` [ruby-dev:50613] " keystonelemur
2018-08-09 2:23 ` [ruby-dev:50614] " manga.osyo
2018-08-09 4:06 ` [ruby-dev:50615] " tim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.issue-14916.20180717170035.158c115d0306d10f@ruby-lang.org \
--to=ruby-dev@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).