子クラスには定数も継承される
へぇと思ったのでメモ。
親クラスで定義された定数は、子クラスにも継承されて修飾なしで参照できる。
クラス名も定数なので、それも出る。
class Hoge ABC = 123 class Fuga end end class Foo < Hoge p constants #=> [:ABC, :Fuga] end
なんだか変な感じである。
また、
Foo → Hoge | | Foo::Bar → Hoge::Bar
こういうクラスを作ろうとすると、以下のようになる。
class Hoge class Bar end end class Foo < Hoge class Bar < Bar # 左右で違うBarになる end end p Foo::Bar.ancestors #=> [Foo::Bar, Hoge::Bar, Object, Kernel, BasicObject]
というか先にこういうことがしたくて、こんな仕様を知った。