Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > modules including other modules

Reply
Thread Tools

modules including other modules

 
 
Tim Olsen
Guest
Posts: n/a
 
      05-29-2007
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

 
Reply With Quote
 
 
 
 
Jeremy Henty
Guest
Posts: n/a
 
      05-29-2007
On 2007-05-29, Tim Olsen <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      05-29-2007


On May 29, 12:25 pm, Jeremy Henty <(E-Mail Removed)> wrote:
> On 2007-05-29, Tim Olsen <(E-Mail Removed)> 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.


 
Reply With Quote
 
Adriano Mitre
Guest
Posts: n/a
 
      01-06-2011
Well, this limitation can be circumvented by runnning the following
after each change in MyModule:

ObjectSpace.each_object(Module) do |m|
if m <= Enumerable
m.module_eval("include MyModule")
end
end


In order to understand the drawbacks of this solution, I suggest reading
http://ola-bini.blogspot.com/2007/07...t-to-have.html

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Adriano Mitre
Guest
Posts: n/a
 
      01-20-2011
Update: the aforementioned solution was encapsulated into a gem which
makes it feels like it were part of the Ruby language.

Synopsis:
Enumerable.module_eval { retroactively_include MyModule }

At http://rubygems.org/gems/retroactive_module_inclusion one may find
the link to the GitHub repository, documentation, bug tracker, etc.

--
Posted via http://www.ruby-forum.com/.

 
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
including general variables in modules Mario Ruiz Ruby 10 03-13-2008 12:54 PM
including modules - ordering question Aryk Grosz Ruby 3 06-27-2007 05:21 PM
Inconsistent(?) when including modules Paulo Jabardo Ruby 2 07-08-2005 08:31 AM
Importing modules from within other modules Tobiah Python 2 09-14-2003 09:18 PM



Advertisments