2013-03-26 34 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 
相關問題