在 Ruby 中调用超级模块中的重写方法

Xin*_*ang 1 ruby methods module super

在以下代码中,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
Run Code Online (Sandbox Code Playgroud)

iCo*_*ime 6

您可以直接使用方法对象

class Me
  include Parent

  def self.just_do
    method(:just_do).super_method.super_method.call
  end
end
Run Code Online (Sandbox Code Playgroud)