external Module.nesting

Discussion in 'Ruby' started by Daniel Brumbaugh Keeney, Jan 18, 2008.

  1. Is there an obvious method for a module like
    A::B::C
    that would result in A::B?

    Module.nesting came to mind, but no I didn't see an obvious way to make it work.

    Daniel Brumbaugh Keeney
     
    Daniel Brumbaugh Keeney, Jan 18, 2008
    #1
    1. Advertising

  2. Daniel Brumbaugh Keeney

    Xavier Noria Guest

    On Jan 19, 2008, at 12:29 AM, Daniel Brumbaugh Keeney wrote:

    > Is there an obvious method for a module like
    > A::B::C
    > that would result in A::B?


    What do you mean?

    You know, modules are objects, constants are stored in modules. Given
    those rules you can put a module almost anywhere. AFAICT what you
    cannot change via API is its name, which is set in stone the first
    time a module is assigned to a constant. That happens in regular
    module definitions, or explicitly like this:

    $ irb
    irb(main):001:0> m = Module.new
    => #<Module:0x20eb6c>
    irb(main):002:0> m.name
    => ""
    irb(main):003:0> M = m
    => M
    irb(main):004:0> m.name
    => "M"
    irb(main):005:0> N = M
    => M
    irb(main):006:0> N.name
    => "M"

    -- fxn
     
    Xavier Noria, Jan 19, 2008
    #2
    1. Advertising

  3. Daniel Brumbaugh Keeney

    Xavier Noria Guest

    On Jan 19, 2008, at 1:07 AM, Xavier Noria wrote:

    > On Jan 19, 2008, at 12:29 AM, Daniel Brumbaugh Keeney wrote:
    >
    >> Is there an obvious method for a module like
    >> A::B::C
    >> that would result in A::B?

    >
    > What do you mean?


    Oh, perhaps you just want something like Module#parent provided by
    Active Support. There the parent is computed by hand from the module
    name:

    def parent
    parent_name = name.split('::')[0..-2] * '::'
    parent_name.empty? ? Object : parent_name.constantize
    end

    where String#constantize is

    def constantize(camel_cased_word)
    unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError, "#{camel_cased_word.inspect} is not a valid
    constant name!"
    end

    Object.module_eval("::#{$1}", __FILE__, __LINE__)
    end

    -- fxn
     
    Xavier Noria, Jan 19, 2008
    #3
    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. Brian Marick
    Replies:
    7
    Views:
    153
    George Marrows
    Apr 14, 2004
  2. gga
    Replies:
    3
    Views:
    165
    William Morgan
    Feb 19, 2005
  3. Brian Mitchell

    Module.nesting and module_eval

    Brian Mitchell, Apr 1, 2005, in forum: Ruby
    Replies:
    1
    Views:
    146
    Yukihiro Matsumoto
    Apr 2, 2005
  4. Trans
    Replies:
    10
    Views:
    312
    Sean O'Halpin
    Sep 16, 2005
  5. Trans
    Replies:
    8
    Views:
    117
Loading...

Share This Page