Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Question about the behavior of reopening a module and including another module.

Reply
Thread Tools

Question about the behavior of reopening a module and including another module.

 
 
Zev Blut
Guest
Posts: n/a
 
      11-23-2004
Hello,

Recently a colleague and I discovered that our understanding of how
modules work, with regards to reopening a module after being included
in a class, seems to be at odds with how Ruby actually handles the
situation.

I am providing an example of code that highlights the situation.
What do you think will happen?
My expectations are given after the code.

------------------------------
module LangDefault
def foo
:LangDefault
end
end

module XLang
include LangDefault
end

module JLang
def foo
:JLang
end
end

class Talk
include XLang
end

tlk = Talk.new
puts tlk.foo

module XLang
include JLang
end

puts tlk.foo
puts Talk.new.foo

class Write
include XLang
end

puts Write.new.foo

puts Talk.included_modules.join(",")
puts Write.included_modules.join(",")

class Talk
include XLang
end

puts Talk.new.foo

------------------------------


I assumed that once XLang included JLang, after the first time Talk
included XLang, that Talk would also recognize this change. Thus, an
instance of Talk's invocation of foo after XLang included JLang would
return :JLang instead of :LangDefault. This is what an instance of
Write's invocation does. Unfortunately, Talk does not do this until I
include XLang in Talk.


Is this a design decision or an implementation issue?
I ask, because I would like to further refine my understanding of
modules and I am looking for techniques for run time extension of a
program. Currently, the above works except that I have to make sure
that a module is extended before it is included in any classes.

Best,
Zev Blut


 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      11-23-2004
Zev Blut schrieb:
> Recently a colleague and I discovered that our understanding of how
> modules work, with regards to reopening a module after being included
> in a class, seems to be at odds with how Ruby actually handles the
> situation.
>
> (example snipped)
>
> Is this a design decision or an implementation issue?


You can find a description here:
http://www.ruby-doc.org/docs/Program...lasses.html#UC (if you have
the paper version see also figure 19.4 on page 247).

By including a module, anonymous proxy classes pointing to the module and its
ancestors are added to the ancestors of the including class. If you alter a
module's ancestors AFTER including the module in a class, the proxy classes
don't reflect this change.

I think it's an implementation issue.

Regards,
Pit


 
Reply With Quote
 
 
 
 
Zev Blut
Guest
Posts: n/a
 
      11-24-2004
Hello,

On Wed, 24 Nov 2004 06:31:18 +0900, Pit Capitain <(E-Mail Removed)> wrote:

> You can find a description here:
> http://www.ruby-doc.org/docs/Program...lasses.html#UC (if
> you have the paper version see also figure 19.4 on page 247).


Thanks for the link, it certainly helps describe how it is implemented.

> By including a module, anonymous proxy classes pointing to the module
> and its ancestors are added to the ancestors of the including class. If
> you alter a module's ancestors AFTER including the module in a class,
> the proxy classes don't reflect this change.
>
> I think it's an implementation issue.


Yeah probably so.
Are there plans to change this in Ruby 2.0?

Best,
Zev




 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      11-24-2004
On Wednesday 24 November 2004 05:24 am, Zev Blut wrote:
| Hello,
|
| On Wed, 24 Nov 2004 06:31:18 +0900, Pit Capitain <(E-Mail Removed)> wrote:
| > You can find a description here:
| > http://www.ruby-doc.org/docs/Program...lasses.html#UC (if
| > you have the paper version see also figure 19.4 on page 247).
|
| Thanks for the link, it certainly helps describe how it is implemented.
|
| > By including a module, anonymous proxy classes pointing to the module
| > and its ancestors are added to the ancestors of the including class. If
| > you alter a module's ancestors AFTER including the module in a class,
| > the proxy classes don't reflect this change.
| >
| > I think it's an implementation issue.
|
| Yeah probably so.
| Are there plans to change this in Ruby 2.0?

It is a rather fundamental problem, so is not likely to get addressed.
Thankfully, it is extremely rare for it to be a problem.

There has been some talk of this on suby-ruby, with a possible solution or
two, but they have performace downsides.

T.


 
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 a module in an included module. Prince Nez Ruby 3 08-18-2009 11:46 AM
Re: IE and Firefox DNS Errors forcing reopening browser Gerry Computer Support 0 02-08-2008 05:40 PM
WriteExcel module, reopening a file dn.perl@gmail.com Perl Misc 2 12-21-2007 02:51 PM
Module to make guerilla (monkey?) / reopening classes easier. Comments? Logan Capaldo Ruby 0 08-13-2006 07:28 PM
How do I return to the top of a file without closing and reopening? Joseph Ellis Perl Misc 1 07-25-2003 05:44 AM



Advertisments