このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

Ruby 3.0 リファレンスマニュアル

instance method Module#<=>

self <=> other -> -1 | 0 | 1 | nil[permalink][rdoc][edit]

self と other の継承関係を比較します。

self と other を比較して、 self が other の子孫であるとき -1、同一のクラス/モジュールのとき 0、 self が other の先祖であるとき 1 を返します。

継承関係にないクラス同士の比較では nil を返します。

other がクラスやモジュールでなければ nil を返します。

[PARAM] other:
比較対象のクラスやモジュール
module Flyable
end
class Bird
  include Flyable
end
class Eagle < Bird
end
class Fish
end
p Bird <=> Flyable      # => -1
p Eagle <=> Bird        # => -1
p Eagle <=> Flyable     # => -1
p Eagle <=> Fish        # => nil
p Fish <=> Eagle        # => nil

p Eagle <=> Object.new  # => nil