Module#ancestors でクラスの継承ツリーを取得することができます。
以下のスクリプトを実行すると、
ruby
1class A
2 def foo
3 puts "A"
4 end
5end
6
7class B < A
8 def foo
9 super
10 puts "B"
11 end
12end
13
14module M
15 def foo
16 super
17 puts "M"
18 end
19end
20
21puts '------------------------'
22puts 'before including module M'
23puts B.ancestors
24
25B.include(M)
26
27puts '------------------------'
28puts 'after including module M'
29puts B.ancestors
結果は、以下のようになります。
------------------------
before including module M
B
A
Object
Kernel
BasicObject
------------------------
after including module M
B
M
A
Object
Kernel
BasicObject
M が B と A の間に、挿入されたことがわかります。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/14 16:44