rubyのクラス同士の継承関係を比較する演算子についてのメモ。

ruby>>

class Foo;end
class Bar<Foo;end
class Baz<Foo;end
[Foo < Bar, Bar < Foo, Foo > Bar, Bar > Foo]
=> [false, true, true, false]

Foo <=> Bar
=> 1

Foo <=> String
=> nil

Bar <=> Foo
=> -1

Bar <=> Baz
=> nil
<<--

また、以下のようにモジュールのinclude関係も取得出来ます。

ruby>>

module Goo;end
class Foo;include Goo end
Foo < Goo
=> true

Foo > Goo
=> false

Goo < Foo
=> false

Goo > Foo
=> true
<<--

posted by genki genki on Sun 25 Jan 2009 at 20:40 with 0 comments