使模块继承自Ruby中的另一个模块

时间:2021-02-25 23:19:42

I'm making a small program for Rails that includes some of my methods I've built inside of a module inside of the ApplicationHelper module. Here's an example:

我正在为Rails创建一个小程序,其中包括我在ApplicationHelper模块内部的模块内部构建的一些方法。这是一个例子:

module Helper
    def time
        Time.now.year
    end
end

module ApplicationHelper
    # Inherit from Helper here...
end

I know that ApplicationHelper < Helper and include Helper would work in the context of a class, but what would you use for module-to-module inherits? Thanks.

我知道ApplicationHelper 和包含helper可以在类的上下文中工作,但是你会使用什么来进行模块到模块的继承?谢谢。

2 个解决方案

#1


24  

In fact you can define a module inside of another module, and then include it within the outer one.

实际上,您可以在另一个模块内部定义一个模块,然后将其包含在外部模块中。

so ross$ cat >> mods.rb
module ApplicationHelper
  module Helper
    def time
      Time.now.year
    end
  end
  include Helper
end

class Test
  include ApplicationHelper
  def run
    p time
  end
  self
end.new.run
so ross$ ruby mods.rb
2012

#2


1  

One potential gotcha is that if the included module attaches class methods, then those methods may be attached to the wrong object.

一个潜在的问题是,如果包含的模块附加了类方法,那么这些方法可能会附加到错误的对象上。

In some cases, it may be safer to include the 'parent' module directly on the base class, then include another module with the new methods. e.g.

在某些情况下,将“父”模块直接包含在基类中可能更安全,然后使用新方法包含另一个模块。例如

module ApplicationHelper
  def self.included(base)
    base.class_eval do
      include Helper
      include InstanceMethods
    end
  end

  module InstanceMethods
    def new_method
      #..
    end
  end

end

The new methods are not defined directly in ApplicationHelper as the include Helper would be run after the method definitions, causing them to be overwritten by Helper. One could alternatively define the methods inside the class_eval block

新方法不是直接在ApplicationHelper中定义的,因为include Helper将在方法定义之后运行,导致它们被Helper覆盖。也可以在class_eval块中定义方法

#1


24  

In fact you can define a module inside of another module, and then include it within the outer one.

实际上,您可以在另一个模块内部定义一个模块,然后将其包含在外部模块中。

so ross$ cat >> mods.rb
module ApplicationHelper
  module Helper
    def time
      Time.now.year
    end
  end
  include Helper
end

class Test
  include ApplicationHelper
  def run
    p time
  end
  self
end.new.run
so ross$ ruby mods.rb
2012

#2


1  

One potential gotcha is that if the included module attaches class methods, then those methods may be attached to the wrong object.

一个潜在的问题是,如果包含的模块附加了类方法,那么这些方法可能会附加到错误的对象上。

In some cases, it may be safer to include the 'parent' module directly on the base class, then include another module with the new methods. e.g.

在某些情况下,将“父”模块直接包含在基类中可能更安全,然后使用新方法包含另一个模块。例如

module ApplicationHelper
  def self.included(base)
    base.class_eval do
      include Helper
      include InstanceMethods
    end
  end

  module InstanceMethods
    def new_method
      #..
    end
  end

end

The new methods are not defined directly in ApplicationHelper as the include Helper would be run after the method definitions, causing them to be overwritten by Helper. One could alternatively define the methods inside the class_eval block

新方法不是直接在ApplicationHelper中定义的,因为include Helper将在方法定义之后运行,导致它们被Helper覆盖。也可以在class_eval块中定义方法