如何在Ruby中将几个模块同时包含到元类和普通类中
我有几个模块,比如Capybara::DSL、RSpec::Matchers、路由器、Common 我希望能够在代码中的任何地方使用这些模块中的方法。目前我试图做:如何在Ruby中将几个模块同时包含到元类和普通类中,ruby,module,Ruby,Module,我有几个模块,比如Capybara::DSL、RSpec::Matchers、路由器、Common 我希望能够在代码中的任何地方使用这些模块中的方法。目前我试图做: module Helper # from http://stackoverflow.com/a/4663029/841064 module ClassMethods include Capybara::DSL include RSpec::Matchers include Router inc
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
您知道一种方法可以使所有这些方法在包含到模块或类中时都可以作为实例方法和类方法访问吗?在要包含模块的类中同时使用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