0

I know by default Ruby modules are not instantiate. However I am using a gem with this singleton interface and I need two instances of it since there are two distinct configurations I need. Is there a way to deep clone or instantiate a second object or even wrap it in another close/module and run a second one with all it's methods?

module Example
  class << self
    def config
      ....
      Base.name = self.name
      ....
    end
  end
end
Teddy
  • 55
  • 2
  • 9
  • 1
  • Also, you are always free to `Example.singleton_class.prepend(Module.new { def config; super; Base.name = Other; end })`. – Aleksei Matiushkin Nov 21 '19 at 12:16
  • Base in a class extending ActiveResource, the interface is here https://github.com/chargify/chargify_api_ares/blob/master/lib/chargify_api_ares/config.rb – Teddy Nov 21 '19 at 13:06
  • FWIW, this question has nothing to do with metaprogramming. – Aleksei Matiushkin Nov 21 '19 at 13:41
  • From my understanding the pattern above uses the metaclass (eigenclass) of ruby to force a singleton pattern on a module, which is why I tagged it metaprogramming :D – Teddy Nov 21 '19 at 13:47
  • Wouldn't plain #clone just work? – TedTran2019 Nov 21 '19 at 18:41
  • If you have a class that's utilizing singleton module and you use #instance on it, you can do Class.clone.instance to get another instance. It's not pretty, but it works. – TedTran2019 Nov 21 '19 at 18:48
  • Cloning seems to work but the singleton still seems to share the Base::ActiveResource instance. I tried looking for a deep clone, one that would effectively marshal it all but i haven't had any luck. – Teddy Nov 22 '19 at 11:57

0 Answers0