首页 > 解决方案 > 在 Ruby 中调用超级模块中的重写方法

问题描述

在以下代码中,Parent#just_do覆盖GrandParent#just_do. 在Me课堂上,我该如何打电话GrandParent#just_do

module GrandParent
  def just_do
    puts "GrandParent"
  end
end

module Parent
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    include GrandParent

    def just_do
      puts "Parent"
    end
  end
end

class Me
  include Parent

  def self.just_do
    super # this calls Parent#just_do
  end
end

标签: rubymethodsmodulesuper

解决方案


您可以直接使用方法对象

class Me
  include Parent

  def self.just_do
    method(:just_do).super_method.super_method.call
  end
end

推荐阅读