Getting Module from Symbol

I want to use methods in different modules depending on commandline switches. So I thought i'd use

module Module1
  module SubModule1
    def foo() end
  end

  module SubModule2
    def foo() end
  end
end

module Module2
  module SubModule1
    def foo() end
  end

  module SubModule2
    def foo() end
  end
end

m1 = Module1
m2 = SubModule1

m1::m2::foo()

to call the correct foo depending on m1 and m2.

This does not work, because when assigning SubModule1 to m2 it is not defined.

Logical conclusion, I want to name the module, not take the module when assigning the variable => I have to use a symbol. But then I do not want to call m2 on the Symbol :Module1, so somehow I have to go from symbol to module.

Is eval the only way to do this, or is there something more elegant?

Thanks,

Brian

···

--
Brian Schröder
http://www.brian-schroeder.de/

I want to use methods in different modules depending on commandline switches. So I thought i'd use
module Module1
  module SubModule1
    def foo() end

       module_function :foo

  end

[...]

m1 = Module1
m2 = SubModule1

m1::m2::foo()

   m1 = Module1
   m2 = m1.const_get(:SubModule1)
   m2::foo

Guy Decoux

Thank you, I knew there was something. But what is the const_get equivalent for object, such that I can use:

m = const_get(:Module1).const_get(:SubModule1)

Regards,

Brian

···

On Sun, 14 Nov 2004 01:19:55 +0900 ts <decoux@moulon.inra.fr> wrote:

> I want to use methods in different modules depending on commandline switches. So I thought i'd use
> module Module1
> module SubModule1
> def foo() end

       module_function :foo

> end

[...]

> m1 = Module1
> m2 = SubModule1

> m1::m2::foo()

   m1 = Module1
   m2 = m1.const_get(:SubModule1)
   m2::foo

Guy Decoux

--
Brian Schröder
http://www.brian-schroeder.de/

m = const_get(:Module1).const_get(:SubModule1)

   m = Object.const_get(:Module1).const_get(:SubModule1)

Object is at the the top of the ruby hierarchy

Guy Decoux

Ah, thanks. I thought that as I were in Kernel, and Kernel is a Object, I could use const_get directly.

But I imagine that it is a module method, and as such the receiver is the class btw. module not the instance (kernel). Correct?

Thanks,

Brian

···

On Sun, 14 Nov 2004 01:33:54 +0900 ts <decoux@moulon.inra.fr> wrote:

> m = const_get(:Module1).const_get(:SubModule1)

   m = Object.const_get(:Module1).const_get(:SubModule1)

Object is at the the top of the ruby hierarchy

Guy Decoux

--
Brian Schröder
http://www.brian-schroeder.de/

Ah, thanks. I thought that as I were in Kernel, and Kernel is a Object,
I could use const_get directly.

at top level self is an instance of Object, this is why it give an error
in your case

uln% ruby -e 'module M end; p self.const_get(:M)'
-e:1: undefined method `const_get' for main:Object (NoMethodError)
uln%

But I imagine that it is a module method, and as such the receiver is
the class btw. module not the instance (kernel). Correct?

well #const_get is defined in Module

but probably you don't need to see what follow :slight_smile:

uln% ruby -e 'module M end; p Kernel.const_get(:M)'
M
uln%

uln% ruby -e 'module M end; p Object.const_get(:M)'
M
uln%

uln% ruby -e 'module M end; p Array.const_get(:M)'
M
uln%

Guy Decoux