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

在Ruby的超级模块中调用重写方法,ruby,methods,module,super,Ruby,Methods,Module,Super,在下面的代码中,Parentjust\u do覆盖了祖父母just\u do。在我的课堂上,我怎么称呼祖父母呢 你真的无法取消覆盖某些内容。在这种特殊情况下,您可以重新导入它,但这可能会产生其他副作用 父对象最好保留原始对象,因为可能需要调用: module GrandParent def just_do puts "GrandParent" end end module Parent def self.included(base) base.extend Clas

在下面的代码中,Parentjust\u do覆盖了祖父母just\u do。在我的课堂上,我怎么称呼祖父母呢


你真的无法取消覆盖某些内容。在这种特殊情况下,您可以重新导入它,但这可能会产生其他副作用

父对象最好保留原始对象,因为可能需要调用:

module GrandParent
  def just_do
    puts "GrandParent"
  end
end

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

  module ClassMethods
    include GrandParent

    # Creates an alias to the original method
    alias_method :grandpa_just_do, :just_do
    def just_do
      puts "Parent"
    end
  end
end

class Me
  include Parent

  def self.just_do
    # Call using the alias created previously
    grandpa_just_do
  end
end

Me.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

    # Creates an alias to the original method
    alias_method :grandpa_just_do, :just_do
    def just_do
      puts "Parent"
    end
  end
end

class Me
  include Parent

  def self.just_do
    # Call using the alias created previously
    grandpa_just_do
  end
end

Me.just_do
您可以直接使用

class Me
  include Parent

  def self.just_do
    method(:just_do).super_method.super_method.call
  end
end
您可以直接使用

class Me
  include Parent

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

好的,不管是什么,如果你不在中间,你可以在中间有多个层。method@XingangHuang,当层数未知,但要调用的方法的所有者模块已知时,你可以写一些与当前问题无关的东西。模块A;def x;'A';终止终止模块B;def x;'B';终止终止模块C;def x;'C′;终止终止Y类;包括A、B、C;def-zc;m=方法:x;而m.owner!=Cm=m.super_方法;终止m、 呼叫;终止终止然后对于y=y.new,y.zA=>A';y、 zB=>B';嗯,不管是哪种父代,都可以在中间有多个层,只要它们不重写同一层。method@XingangHuang,当层数未知,但要调用的方法的所有者模块已知时,你可以写一些与当前问题无关的东西。模块A;def x;'A';终止终止模块B;def x;'B';终止终止模块C;def x;'C′;终止终止Y类;包括A、B、C;def-zc;m=方法:x;而m.owner!=Cm=m.super_方法;终止m、 呼叫;终止终止然后对于y=y.new,y.zA=>A';y、 zB=>B';y、 zC=>C。