[ruby-core:93046] [Ruby trunk Bug#15857] <=> の右辺が <=> を実装していない場合の振る舞い

From: merch-redmine@...
Date: 2019-06-11 17:18:04 UTC
List: ruby-core #93046
Issue #15857 has been updated by jeremyevans0 (Jeremy Evans).

File complex-real-spaceship-v3.patch added

shuujii (Shuji KOBAYASHI) wrote:
> @jeremyevans0 Thank you for your comment. About `Complex`, I agree with most of v2 patch, but I have some comments.
> 
> - Should methods from `Comparable` that are currently disabled be enabled?

I don't think so, but I don't have a strong opinion.  If you would like those added, please submit a separate feature request for that.

> - `rb_undef_method(rb_cComplex, "<=>")` is unneeded.
> - `idCmp` can be used instead of `id_spaceship`.

Thank you, I made both of these changes in the attached v3 patch.

> About other than `Complex`, `<=>` of some classes, such as `String` and `Time`, will fail if LHS does not implement `<=>`. It's better to return `nil` in this case too, because whether it's comparable or not and whether LHS implements `<=>` or not is essentially irrelevant, I think.

I agree, but please submit a separate bug report for that as it is unrelated to `Complex#<=>`.

----------------------------------------
Bug #15857: <=> の右辺が <=> を実装していない場合の振る舞い
https://bugs.ruby-lang.org/issues/15857#change-78440

* Author: shuujii (Shuji KOBAYASHI)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
`<=>` の右辺が `<=>` を実装していないとき、`nil` が返却される場合と例外が発生する場合があり一貫性がないように思えるのですが、意図的でしょうか。

```ruby
  0 <=> 0i               #=> NoMethodError (undefined method `<=>' for (0+0i):Complex)
  0 <=> BasicObject.new  #=> nil
 :a <=> 0i               #=> nil
"a" <=> 0i               #=> NoMethodError (undefined method `<=>' for (0+0i):Complex)

```

なお、`0 <=> 0i` に関しては、`0 == 0i` は `true` になるのでそれとも一貫性がないように思えるのもやや気になりました。

---Files--------------------------------
complex-real-spaceship.patch (8.75 KB)
complex-real-spaceship-v2.patch (8.08 KB)
complex-real-spaceship-v3.patch (7.79 KB)


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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next