Ruby 命名空间混合方法

Ruby namespace mixin methods

我有 类 这样的命名空间:

module SocialSearch
  module Scrapers
    class TwitterScraper
      # ...
    end
  end
end

我想在 Scrapers 中创建一个方法,该方法将在命名空间内的所有 类 中扩展,但无需在每个 [=] 中手动执行 include Scrapers 21=] 鉴于它们在 Scrapers 命名空间内。

谢谢!

尝试使用继承:

class Scrapers
  # your common methods for all scrapers
end

class TwitterScraper < Scrapers
  # your twitter-related scraper setup; methods from Scrapers are visible here
end

class FacebookScraper < Scrapers
  # your twitter-related scraper setup; methods from Scrapers are visible here
end
SocialSearch::Scrapers.constants.each do
  |k| k = SocialSearch::Scrapers.const_get(k)
  k.extend(SocialSearch::Scrapers)if k.instance_of?(Class)
end

SocialSearch::Scrapers.constants.each do
  |k| k = SocialSearch::Scrapers.const_get(k)
  k.include(SocialSearch::Scrapers)if k.instance_of?(Class)
end