[#66126] Creation/Conversion methods/functions table for Ruby types — SASADA Koichi <ko1@...>
Hi,
5 messages
2014/11/07
[#66248] [ruby-trunk - Feature #10423] [PATCH] opt_str_lit*: avoid literal string allocations — normalperson@...
Issue #10423 has been updated by Eric Wong.
3 messages
2014/11/13
[#66595] [ruby-trunk - Bug #10557] [Open] Block not given when the argument is a string — bartosz@...
Issue #10557 has been reported by Bartosz Kopinski.
3 messages
2014/11/30
[ruby-core:66241] [ruby-trunk - Feature #10505] [Open] [PATCH 2/n] 同相条件を扱いたい./Object#eql? with block. (ja/en)
From:
mail@...
Date:
2014-11-13 00:31:08 UTC
List:
ruby-core #66241
Issue #10505 has been reported by gogo tanaka.
----------------------------------------
Feature #10505: [PATCH 2/n] 同相条件を扱いたい./Object#eql? with block. (ja/en)
https://bugs.ruby-lang.org/issues/10505
* 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/