Getting Module from Symbol

Discussion in 'Ruby' started by Brian Schröder, Nov 13, 2004.

  1. 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/
    Brian Schröder, Nov 13, 2004
    #1
    1. Advertising

  2. Brian Schröder

    ts Guest

    >>>>> "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
    ts, Nov 13, 2004
    #2
    1. Advertising

  3. On Sun, 14 Nov 2004 01:19:55 +0900
    ts <> 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/
    Brian Schröder, Nov 13, 2004
    #3
  4. Brian Schröder

    ts Guest

    >>>>> "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
    ts, Nov 13, 2004
    #4
  5. On Sun, 14 Nov 2004 01:33:54 +0900
    ts <> 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/
    Brian Schröder, Nov 13, 2004
    #5
  6. Brian Schröder

    ts Guest

    >>>>> "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
    ts, Nov 13, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor
    Replies:
    3
    Views:
    5,917
    Victor
    Oct 1, 2003
  2. baumann@pan
    Replies:
    1
    Views:
    732
    Richard Bos
    Apr 15, 2005
  3. Replies:
    8
    Views:
    433
  4. Song Ma
    Replies:
    2
    Views:
    221
    Charles Oliver Nutter
    Jul 20, 2008
  5. Replies:
    6
    Views:
    1,737
Loading...

Share This Page