ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:50593] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
@ 2018-07-17 17:00 ` manga.osyo
  2018-08-04 16:22 ` [ruby-dev:50602] " keystonelemur
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: manga.osyo @ 2018-07-17 17:00 UTC (permalink / raw)
  To: ruby-dev

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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-dev:50602] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
  2018-07-17 17:00 ` [ruby-dev:50593] [Ruby trunk Feature#14916] Proposal to add Array#=== manga.osyo
@ 2018-08-04 16:22 ` keystonelemur
  2018-08-06 14:32 ` [ruby-dev:50608] " eregontp
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: keystonelemur @ 2018-08-04 16:22 UTC (permalink / raw)
  To: ruby-dev

Issue #14916 has been updated by baweaver (Brandon Weaver).


I have found this gem, which may be useful here: https://www.rubydoc.info/gems/anything/0.0.1

It would allow you to do this:

```
def fizzbuzz n
  case [n % 3, n % 5]
  # n % 3 === 0 && n % 5 === 0
  when [0, 0]
    "FizzBuzz"
  # n % 3 === 0
  when [0, Anything]
    "Fizz"
  # n % 5 === 0
  when [Anything, 0]
    "Buzz"
  else
    n
  end
end
```

Would a wildcard be a potential consideration for later?

----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-73314

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-dev:50608] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
  2018-07-17 17:00 ` [ruby-dev:50593] [Ruby trunk Feature#14916] Proposal to add Array#=== manga.osyo
  2018-08-04 16:22 ` [ruby-dev:50602] " keystonelemur
@ 2018-08-06 14:32 ` eregontp
  2018-08-08 13:21 ` [ruby-dev:50612] " manga.osyo
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: eregontp @ 2018-08-06 14:32 UTC (permalink / raw)
  To: ruby-dev

Issue #14916 has been updated by Eregon (Benoit Daloze).


I think there is a potential incompatibility here, due to changing the behavior of #===.
The fact `Module#===` doesn't return true for mod === mod exacerbates the issue.

For instance,

~~~ ruby
v = [String, 42]
case v
when [String, 42]
 p :ok
else
 :incompatible
end
~~~

Are there no existing use of case/when with an Array in when?

----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-73339

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-dev:50612] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2018-08-06 14:32 ` [ruby-dev:50608] " eregontp
@ 2018-08-08 13:21 ` manga.osyo
  2018-08-08 20:46 ` [ruby-dev:50613] " keystonelemur
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: manga.osyo @ 2018-08-08 13:21 UTC (permalink / raw)
  To: ruby-dev

Issue #14916 has been updated by osyo (manga osyo).

File array_eqq.patch added

> I think there is a potential incompatibility here, due to changing the behavior of #===.
> The fact Module#=== doesn't return true for mod === mod exacerbates the issue.

こちらですが、互換性を壊さないように `===` だけではなくて `==` でも比較するようにし、いずれかの比較演算子の結果が真であれば `true` を返すように変更してみました。

```ruby
String === String
# => false
[String] === [String]
# => true
[/aaa/] === [/aaa/]
# => true

v = [String, 42]
case v
when [String, 42]
 p :ok
else
 :incompatible
end
# => :ok

```

----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-73380

* 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)
array_eqq.patch (3.2 KB)


-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-dev:50613] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2018-08-08 13:21 ` [ruby-dev:50612] " manga.osyo
@ 2018-08-08 20:46 ` keystonelemur
  2018-08-09  2:23 ` [ruby-dev:50614] " manga.osyo
  2018-08-09  4:06 ` [ruby-dev:50615] " tim
  6 siblings, 0 replies; 7+ messages in thread
From: keystonelemur @ 2018-08-08 20:46 UTC (permalink / raw)
  To: ruby-dev

Issue #14916 has been updated by baweaver (Brandon Weaver).


I've noticed that this will always return false if the other value is not an array:

(line 4011 of patch)
```ruby
if (!RB_TYPE_P(ary2, T_ARRAY)) return Qfalse;
```

Have we considered potentially checking if the object responds to `to_ary`, and if so coercing it in the comparison?

This behavior is present in `IPAddr`, which coerces the right-hand value to an `IPAddr` before comparison.

I believe this would, while taking a minor speed hit, give much greater flexibility and duck-typing compatibility to this feature.

----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-73385

* 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)
array_eqq.patch (3.2 KB)


-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-dev:50614] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2018-08-08 20:46 ` [ruby-dev:50613] " keystonelemur
@ 2018-08-09  2:23 ` manga.osyo
  2018-08-09  4:06 ` [ruby-dev:50615] " tim
  6 siblings, 0 replies; 7+ messages in thread
From: manga.osyo @ 2018-08-09  2:23 UTC (permalink / raw)
  To: ruby-dev

Issue #14916 has been updated by osyo (manga osyo).

File array_eqq.patch added

> I believe this would, while taking a minor speed hit, give much greater flexibility and duck-typing compatibility to this feature.

OK, support call `to_ary`.



----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-73389

* 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)
array_eqq.patch (3.2 KB)
array_eqq.patch (3.41 KB)


-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-dev:50615] [Ruby trunk Feature#14916] Proposal to add Array#===
       [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2018-08-09  2:23 ` [ruby-dev:50614] " manga.osyo
@ 2018-08-09  4:06 ` tim
  6 siblings, 0 replies; 7+ messages in thread
From: tim @ 2018-08-09  4:06 UTC (permalink / raw)
  To: ruby-dev

Issue #14916 has been updated by timriley (Tim Riley).


Like I just mentioned in https://bugs.ruby-lang.org/issues/14869#note-13, would you consider changing this to run the explicit `#to_a` converter as well as (or instead of) the implicit `#to_ary`? This would make the matching even more flexible, since it could be using with objects that want to provide an interface for conversion into an array without necessarily pretending to "be" one (as is the case for `#to_ary`).

----------------------------------------
Feature #14916: Proposal to add Array#===
https://bugs.ruby-lang.org/issues/14916#change-73392

* 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)
array_eqq.patch (3.2 KB)
array_eqq.patch (3.41 KB)


-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-08-09  4:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-14916.20180717170035@ruby-lang.org>
2018-07-17 17:00 ` [ruby-dev:50593] [Ruby trunk Feature#14916] Proposal to add Array#=== manga.osyo
2018-08-04 16:22 ` [ruby-dev:50602] " 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

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).