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

From: merch-redmine@...
Date: 2019-06-05 04:55:13 UTC
List: ruby-core #92970
Issue #15857 has been updated by jeremyevans0 (Jeremy Evans).

File complex-real-spaceship.patch added

I agree that this is a bug.  `Complex#<=>` should be implemented, even if it did return `nil` for all arguments.

However, I think Complex numbers with an imaginary part of zero should be treated as real numbers, and that `Complex#real?` should return `true` for such numbers.  Likewise, `Complex#<=>` should return 1, 0, or -1 if both the receiver and the argument are real numbers (treating Complex instances with an imaginary part of zero as real numbers).  However, if the receiver or the argument is not a real number, then it should return `nil`.

Attached is a patch that implements the behavior described above.

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

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


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