【问题标题】:Overloading a method in an ActiveSupport::Concern重载 ActiveSupport::Concern 中的方法
【发布时间】:2012-12-28 22:04:06
【问题描述】:

我怎么会有这样的担忧:

module Concerns
  module MyConcern
    extend ActiveSupport::Concern
    ...
    def my_concern_magic(arg0,arg1)
      #exciting stuff here
    end
  end 
end 

包含在重载my_concern_magic 的模型中?例如。

class User
  include Concerns::MyConcern
  ...
  def my_concern_magic(arg0)
    arg1 = [1,2,3]
    my_concern_magic(arg0,arg1)
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby overloading activesupport


    【解决方案1】:

    由于包含模块会将其插入祖先链中,因此您只需调用super

    class User
      include Concerns::MyConcern
    
      def my_concern_magic(arg0)
        arg1 = [1, 2, 3]
        super(arg0, arg1)
      end
    end
    

    【讨论】:

    • 谢谢安德鲁!像魅力一样工作。
    猜你喜欢
    • 2014-04-28
    • 2013-05-26
    • 1970-01-01
    • 2014-01-26
    • 2012-09-14
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    相关资源
    最近更新 更多