0

我有几个模块,比如Capybara::DSL, RSpec::Matchers, Router, Common

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

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

您是否知道一种方法可以在将所有这些方法包含到模块或类中时作为实例和类方法访问它们?

4

1 回答 1

-1

在要包含模块的类中include使用两者如何?extend

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
于 2013-03-26T10:16:59.157 回答