使模块继承自Ruby中的另一个模块 [英] Making a module inherit from another module in Ruby

查看:121
本文介绍了使模块继承自Ruby中的另一个模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

module Helper
    def time
        Time.now.year
    end
end

module ApplicationHelper
    # Inherit from Helper here...
end

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

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.

推荐答案

在实际上你可以定义一个模块的另一模块的内部,并且然后<强>包括在外部。

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

这篇关于使模块继承自Ruby中的另一个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆