Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to method in module with same name as class

Tags:

ruby

I have a class and a module which have the same names:

module Pushover
  def configure
    ..
  end
end

module MyModule
  class Pushover
    def blah
      Pushover.configure
    end
  end
end

This doesn't work because the Pushover.configure call directs to the containing class. Now, an obvious fix would be to rename the class. However, the Module is from a gem and the class conforms to a naming convention required in a DSL. So ideally they should both stay the same. I could also create a second helper class and call via that, but that all seems a little hacky. My preferred solution would be to directly reference the module method.

All the existing questions around this area seem to be disambiguating in the opposite direction - i.e. they want to get the class reference not the module.

Is there any way for me to inform Ruby that I mean the module rather than the class when I specify Pushover?

like image 208
Steve N Avatar asked Nov 11 '12 17:11

Steve N


1 Answers

If you don't want to look up the constant relative to the current scope, just use an absolute path:

::Pushover.configure
like image 190
Jörg W Mittag Avatar answered Oct 05 '22 18:10

Jörg W Mittag