2013-03-26 35 views
0

我有几个模块,比如说Capybara::DSL,RSpec::Matchers,Router,Common如何在Ruby中包含几个模块到元类和普通类

我希望能够在我的代码使用方法从这些模块几乎从任何地方。目前,我试图做的事:

module Helper 
    # from http://stackoverflow.com/a/4663029/841064 
    module ClassMethods 
    include Capybara::DSL 
    include RSpec::Matchers 
    include Router 
    include Common 
    end 

    include Capybara::DSL 
    include RSpec::Matchers 
    include Router 
    include Common 

    extend ClassMethods 

    def self.included(other) 
    other.extend(ClassMethods) 
    end 
end 

然后,我想用它作为:

module A 
    include Helper 

    class << self 
    # all methods from 4 modules are available in all methods here 
    end 

    # all methods from 4 modules are available in all methods here 
end 


class B 
    include Helper 

    class << self 
    # all methods from 4 modules are available in all methods here 
    end 

    # all methods from 4 modules are available in all methods here. But they aren't available here 
end 

你知道的方法来获取所有这些,当他们有实例和类方法访问的方法包括以模块或类?

回答

-1

如何在要包含模块的类中使用includeextend

module Helper 
    include Capybara::DSL 
    include RSpec::Matchers 
    include Router 
    include Common 
end 

module A 
    # Gives methods on instance 
    include Helper 

    # Gives methods on class 
    extend Helper 

    #... 
end 
相关问题