简体   繁体   English

从类中调用包含模块的方法

[英]Call method of included module from class

I have a use case where I have class A which includes module B . 我有一个用例,其中有包含module B class A

class A
  include B

  def do_one_thing
    # override module's method. do something different instead
  end

  def do_another_thing
    # Call `do_one_thing` from here,
    # but call the module's method, not the one I overrode above.
  end
end

module B
  included do
    def do_one_thing
      # ...
    end
  end

  # some other methods
end

As shown above, I'm calling do_one_thing from do_another_thing . 如上图所示,我打电话do_one_thingdo_another_thing My problem is that I need to call the module's method (ie the super method). 我的问题是我需要调用模块的方法(即super方法)。 Is this possible in Rails? 在Rails中可能吗?

To property use the included method, you'll need your B module to extend ActiveSupport::Concern but that won't give you the behaviour you want. 要使用所included方法进行属性设置,您将需要B模块extend ActiveSupport::Concern但这不会为您提供所需的行为。

If I were you I'd abandon that pattern and use simple native Ruby module patterns: 如果您是我,我将放弃该模式,而使用简单的本机Ruby模块模式:

module B    
  def do_one_thing
    puts 'in module'
    # ...
  end

  # some other methods
end

class A
  include B

  def do_one_thing  
    super  
    puts 'in class'
    # override module's method. do something different instead
  end

  def do_another_thing
    do_one_thing
    # Call `do_one_thing` from here,
    # but call the module's method, not the one I overrode above.
  end
end

A.new.do_one_thing

The above code will correctly use the module inheritance you are looking for. 上面的代码将正确使用您要查找的模块继承。

Read more about Ruby module inheritance here 在此处阅读有关Ruby模块继承的更多信息

You can 'save' included method before override 您可以在覆盖之前“保存”包含的方法

module B
  extend ActiveSupport::Concern

  included do
    def do_one_thing
      puts 'do_one_thing'
    end
  end
end

class A
  include B

  alias_method :old_do_one_thing, :do_one_thing
  def do_one_thing
    puts "I'd rather do this"
  end

  def do_another_thing
    old_do_one_thing
  end
end

a= A.new
a.do_one_thing
a.do_another_thing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从包含在rails中的父类中的模块中调用父类的子类的私有方法? - Is it okay to call a private method of a parent class's subclass from a module which is included in the parent class in rails? 如何检查method_defined? 来自 class 中使用的模块,其中包含模块后定义的方法 - How to check method_defined? from a module thats used in class with a method defined after the module is included 在Ruby的类中找不到包含的Module方法 - Included Module method not found in class in Ruby 为什么在RSPEC中对于包含模块中的方法调用的未定义方法? - Why undefined method in RSPEC for a call to method in an included module? 类SessionsHelper中的行为未包含在类中 - Behavior from module SessionsHelper is not included in class 我可以像在包含的模块中那样直接调用实例方法吗? - Can I call instance method directly like in included module? 在包含在类中的Module中使用define_method? - Using define_method inside a Module that gets included inside a Class? 从模块mixin(rails)的实例方法内部调用类方法 - call a class method from inside an instance method from a module mixin (rails) 从包含的同名Module方法中获取Model属性 - Getting Model attribute from included Module method of same name 如何在模块中调用特定于类的方法 - How to call a class specific method in a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM