Getting Module from Symbol

B

Brian Schröder

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
 
T

ts

"B" == Brian =?ISO-8859-15?Q?Schr=F6der?= <Brian> writes:

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

module_function :foo

B> end

[...]

B> m1 = Module1
B> m2 = SubModule1

B> m1::m2::foo()

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


Guy Decoux
 
B

Brian Schröder

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

module_function :foo

B> end

[...]

B> m1 = Module1
B> m2 = SubModule1

B> 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
 
T

ts

"B" == Brian =?ISO-8859-15?Q?Schr=F6der?= <Brian> writes:

B> 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
 
B

Brian Schröder

B> 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
 
T

ts

"B" == Brian =?ISO-8859-15?Q?Schr=F6der?= <Brian> writes:

B> Ah, thanks. I thought that as I were in Kernel, and Kernel is a Object,
B> 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%


B> But I imagine that it is a module method, and as such the receiver is
B> 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 :)


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top