ancestorsとsuper

Module#ancestorsを修正するとsuperは変化するか、という話題です。

class GrandParent
  def hello; puts "GrandParent"; end
end

class Parent < GrandParent
  def hello; super; puts "Parent"; end
end

class Child < Parent
  def hello; super; puts "Child"; end
end

Child.new.hello
                  #=> GrandParent
                  #   Parent
                  #   Child

a = Child.ancestors
p a               #=> [Child, Parent, GrandParent, Object, Kernel]



a[1], a[2] = a[2], a[1]
p a               #=> [Child, GrandParent, Parent, Object, Kernel]

Child.new.hello
                  #=> GrandParent
                  #   Parent
                  #   Child

p Child.ancestors.object_id   #=> 21786840
p Child.ancestors.object_id   #=> 21786780

結論、ancestorsをいじってもsuperは変化しません。そもそもancestorsは毎回新しい配列がnewされるようです。