From: Koji Arai Date: 2002-01-12T15:03:01+09:00 Subject: [ruby-dev:15677] Module#<=> 新井です。 親子関係にないクラス同士の比較で p Class.new <=> Class.new => ruby 1.7.2 (2002-01-05) [i586-linux] 1 は、常に 1 です。他の不等号演算子では、常に false です。 p Class.new < Class.new p Class.new > Class.new => ruby 1.7.2 (2002-01-05) [i586-linux] false false これは、仕様でしょうか?マニュアルにどう書くべきか迷っています。 | --- self <=> other | | (({self})) と (({other})) を比較して、 | (({self})) が (({other})) の子孫であるとき -1、 | 同一のクラス/モジュールのとき 0、 | (({self})) が (({other})) の先祖であるとき 1 | | を返します。 | 親子関係にないクラス同士の比較ではその動作は不定です | ((-あらい 2002-01-12: たぶん。仕様上不定。実装では特定といったところ-))。 | | module Foo | end | class Bar | include Foo | end | class Baz < Bar | end | class Qux | end | p Bar <=> Foo # => -1 | p Baz <=> Bar # => -1 | p Baz <=> Foo # => -1 | p Baz <=> Qux # => 1 | p Qux <=> Baz # => 1 | | --- self < other | --- self <= other | --- self > other | --- self >= other | | 比較演算子。(({self})) が ((|other|)) の子孫である時、 | (({self < other})) が成立します。 | | 親子関係にないクラス同士の比較ではいずれの関係も false を返します。 | | module Foo | end | class Bar | include Foo | end | class Baz < Bar | end | class Qux | end | p Bar < Foo # => foo | p Baz < Bar # => foo | p Baz < Foo # => foo | p Baz < Qux # => false | p Baz > Qux # => false -- 新井康司 (Koji Arai)