modules including other modules

Discussion in 'Ruby' started by Tim Olsen, May 29, 2007.

  1. Tim Olsen

    Tim Olsen Guest

    Hello,

    It appears that including module A inside another module B can cause
    problems for classes that have already included A. What seems to
    happen is that a class can only see the methods from module B if that
    class include module A before module A includes module B.

    For example,

    module A

    def a
    puts "a"
    end

    end

    module B

    def b
    puts "b"
    end

    end

    class C

    include B

    def c
    puts "c"
    end

    end

    module B

    include A

    def b2
    puts "b2"
    end
    end


    C.new.c
    C.new.b
    C.new.b2
    C.new.a

    produces

    tolsen@neurofunk:~/svn/limeade$ /var/tmp/tst.rb
    c
    b
    b2
    /var/tmp/tst.rb:42: undefined method `a' for #<C:0x2ac204fe7180> (NoMethodError)

    Notice I can define b2() after C has already included B but including
    A into B does nothing for C.

    Any idea what's going on here?

    Thanks,
    Tim
     
    Tim Olsen, May 29, 2007
    #1
    1. Advertising

  2. Tim Olsen

    Jeremy Henty Guest

    On 2007-05-29, Tim Olsen <> wrote:

    > It appears that including module A inside another module B can cause
    > problems for classes that have already included A. What seems to
    > happen is that a class can only see the methods from module B if
    > that class include module A before module A includes module B.


    It's a known problem that is apparently almost impossible to fix. See
    http://eigenclass.org/hiki/The double inclusion problem .

    Unless there's been some development that I've failed to Google up,
    your only option is "Don't Do That!". :-( Unless Ruby 1.9 handles
    things better.

    Regards,

    Jeremy Henty
     
    Jeremy Henty, May 29, 2007
    #2
    1. Advertising

  3. Tim Olsen

    Trans Guest

    On May 29, 12:25 pm, Jeremy Henty <> wrote:
    > On 2007-05-29, Tim Olsen <> wrote:
    >
    > > It appears that including module A inside another module B can cause
    > > problems for classes that have already included A. What seems to
    > > happen is that a class can only see the methods from module B if
    > > that class include module A before module A includes module B.

    >
    > It's a known problem that is apparently almost impossible to fix. Seehttp://eigenclass.org/hiki/The+double+inclusion+problem.


    Also know as the "Dynamic Inclusion Problem" or just the "Inclusion
    Problem".

    > Unless there's been some development that I've failed to Google up,
    > your only option is "Don't Do That!". :-( Unless Ruby 1.9 handles
    > things better.


    While, it would be great if someone did find a way, from what I
    understand its too difficult to fix given how Ruby works, so don't
    hold your breath.

    T.
     
    Trans, May 29, 2007
    #3
  4. Adriano Mitre, Jan 6, 2011
    #4
  5. Adriano Mitre, Jan 20, 2011
    #5
    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. Replies:
    3
    Views:
    641
  2. Tobiah
    Replies:
    2
    Views:
    322
    Tobiah
    Sep 14, 2003
  3. Elliot J
    Replies:
    4
    Views:
    394
    Elliot J
    Aug 6, 2007
  4. stefven blonqhern

    two classes #including each other

    stefven blonqhern, Aug 24, 2007, in forum: C++
    Replies:
    7
    Views:
    381
    Pete Becker
    Aug 25, 2007
  5. Raman
    Replies:
    5
    Views:
    382
    Lew Pitcher
    Jun 7, 2008
Loading...

Share This Page