Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Getting Module from Symbol

Reply
Thread Tools

Getting Module from Symbol

 
 
Brian Schröder
Guest
Posts: n/a
 
      11-13-2004
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/



 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      11-13-2004
>>>>> "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




 
Reply With Quote
 
 
 
 
Brian Schröder
Guest
Posts: n/a
 
      11-13-2004
On Sun, 14 Nov 2004 01:19:55 +0900
ts <(E-Mail Removed)> wrote:

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


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

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



 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      11-13-2004
>>>>> "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






 
Reply With Quote
 
Brian Schröder
Guest
Posts: n/a
 
      11-13-2004
On Sun, 14 Nov 2004 01:33:54 +0900
ts <(E-Mail Removed)> wrote:

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


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


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



 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      11-13-2004
>>>>> "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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Encryption Package) error: cannot find symbol symbol: class BaseNCode clusardi2k@aol.com Java 6 08-29-2012 08:33 PM
Why ":symbol" failed but 'symbol' successed with JRuby 1.0.3? Song Ma Ruby 2 07-20-2008 04:08 AM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
what's differnece between #ifdef symbol and #if defined(symbol) baumann@pan C Programming 1 04-15-2005 08:25 AM
Getting module object by name in C extension module Ilariu Raducan Python 2 07-14-2004 10:56 AM



Advertisments