Issue #10505 has been updated by gogo tanaka.


@Shota Fukumori san

Thank you for your comment :)

I got your point, as you said `a % 2 == b % 2` is tottaly obvious for anybody, 
On the other hand, `a.eql?(b) { |n| n % 2 }` looks little bit strange.
(note: I belive `a.eql?(b) { |n| n % 2 }` is more readable if we've know `eql?` takes an block and behaves like so)

Shota Fukumori wrote:
> IMO, I don't like such shorthand supports *only* writability.

For this point, my motivation is come by *not only* writability, but also some point I'm gonna explain below.
As I said, it can be helpful for readable 

```ruby
obj1.eql?(obj2) { |o| func(o.method1.method2) }
```

is more readable than

```ruby
func(obj1.method1.method2) == func(obj2.method1.method2)
```

(そもそもこんなコードが生まれる事がいけないきもしますが)

And in this case, we can make every piece of knowledge a single.(you may call it DRY)
It can be helpful for out maintaining.

----------------------------------------
Feature #10505: [PATCH 2/n] 同相条件を扱いたい./Object#eql? with block. (ja/en)
https://bugs.ruby-lang.org/issues/10505#change-49924

* Author: gogo tanaka
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: 
----------------------------------------
English follows japanese

## 文脈

両辺に同じ処理を施してその同値性を確かめたいという場面は往々にしてあると思います.

```ruby
a % 2 == b % 2

a.abs == b.abs

"RUBY".downcase == "Ruby".downcase
```

この際、施す処理を両辺に書くのは嫌だというのが今回のチケットの主たるモチベーションになります.

特に長い処理ともなると尚更だと思います.
(そもそも長い処理を両辺に施すコンテキストがイカン!という批判は甘んじて受け入れますが)

## 提案

施す処理を各オブジェクトの`#eql?`に対してブロックとして渡せるようにしたいです.

```ruby
# 2を法とする合同式
a.eql?(b) { |n| n % 2 }

# 意味論的にあまり好きでないですが、ブロックをシンボルで渡す事も可能とします.
"RUBY".eql?("Ruby", &:downcase)
```

## 懸念点
* block処理後は`#===`で比較するので挙動に若干の違和感.

```ruby
1.eql?(1.0, &:itself)
# => true

1.eql?(1.0)
# => false
```

* 既存のコードで`#eql?`にブロックを渡している可能性がある?

変更の範囲も広くなりそうなのと見積もれてない懸念点がある気がしたので、ひとまず`Numeric#eql?`だけ実装してみました
しばらく様子を見て問題がなければ他のオブジェクト群に対しても実装したいかとも思います.

よろしくお願い致します.
## Motivation

We often encounter a situation where we need to compare both operands after certain function apply for these.

```ruby
a % 2 == b % 2

a.abs == b.abs

"RUBY".downcase == "Ruby".downcase
```

I am not willing to write same function both operands.
This is main motivation.

## Proposal

Now if the optional block is given, compare between results of running block for both operands.

```ruby
# check wether `a` and `b` are congruent modulo 2 or not
a.eql?(b) { |n| n % 2 }

"RUBY".eql?("Ruby", &:downcase)
```

I've found similar issue [here](https://bugs.ruby-lang.org/issues/10426)

For now, I've implemented for `Numeric#eql?`
If it looks like good for you, I'm gonna implement for other Classes

## Concern
* We'll compare by using `#===` after passed to block. so it may look like weird.

```ruby
1.eql?(1.0, &:itself)
# => true

1.eql?(1.0)
# => false
```


Thanks, gogo.

---Files--------------------------------
Update_Numeric#eql_.patch (1.23 KB)
Add_tests_for_Numeric#eql_.patch (748 Bytes)


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