The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Shutting the Scary Door

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Trans Onoma

Posts: 63
Nickname: transfire
Registered: Aug, 2005

Trans Onoma has programming for too long.
Shutting the Scary Door Posted: Aug 6, 2005 2:34 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Trans Onoma.
Original Post: Shutting the Scary Door
Feed Title: TRANSONOMA
Feed URL: http://feeds.feedburner.com/transonoma
Feed Description: A Light Green Agile Programming and SciTech Blog
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Trans Onoma
Latest Posts From TRANSONOMA

Advertisement
So I've heard Matz considers Ruby's singleton classes an implementation feature, not a design feature. While he wants singleton methods, he doesn't necessarily want the "virtual" classes that currently make them possible. That's interesting.

Today I was thinking more about the idea of self-shimmying, which has simple merits in furthering the DRY principle. But as I was exploring a particular avenue in conjuction with this consideration of Matz', I started to see some alternate scenarios for simplifying the class/module design.

Currently modules are essentially classes. It is merely by some arbitrary limitations that they differ at all. We really don't need the distinction and could easily do everything we do now with classes alone. So, if there's a good use, why not make modules something truly unique from classes? As it so happens there is: singleton classes. Rather then use a singleton to represent the "class-level" of a class, use a module.

Now even though all modules will be effected by this design change, there is still one important minor distinction I'm going to go ahead and make upfront. Modules that serve in place of singleton classes do not have names. Instead, they are referred to via the class or object they "meta" for, so to speak. B/c of this I'll introduce the keyword 'meta', instead of 'module', for this specific use of replacing singleton classes in order to spare confusion and make the syntax more intuitive.


meta X
def mod_meth ; 'module method' ; end
end
class X
mod_meth #=> "module_method"
def inst_meth ; 'instance method' ; end
end


Keep in mind that meta is just another module. Its distinction as 'meta' is simply ot indicate that it is working for a class to provide its "meta methods". Of course, to fully replace singleton classes the same can be done for objects.


o = X.new
meta o
def mod_meth ; super + '!' ; end
end
o.mod_meth #=> "module method!"


Now, the effect all this has on modules in general is that they would no longer have a "class-level", or better their class-level === instance-level. In effect they always have 'extend self' implied. In so being they are simply one-fold encapsulations.


module X
def mod_meth ; 'module method' ; end
mod_meth #=> "module_method"
end


From another perspective, prototypical objects:


module X
def mod_meth ; 'module method' ; end
end
X.mod_method #=> "module method"


Likewise classes themselves become easier to understand --they are strictly two-fold entities with clearly separated meta and instance levels. They no longer have the "scary door" behind them.

Fundamentally what changes here is that a module is no longer the superclass of a class. Rather a class stands on it's own. What was:


.------------------.
| |
Object---->(Object) |
^ ^ ^ ^ |
| | | | |
| | .-----' '---------. |
| | | | |
| '-----------+ | |
| | | | |
.------' | Module--->(Module)-----> "Scary Door"
| | ^ ^ |
| | | | |
| | | | |
| | Class---->(Class)-----> "Scary Door"
| | ^ |
| | | |
| | '----------------'
| |
OtherClass-->(OtherClass)----------------------> "Scary Door"


Becomes:


.------------. .--------.
v | v |
Object---->(ObjectModule)------'
^ ^ ^ ^ ^
| | | | |
| | | | |
.----------------' | | | |
| | | | '----------------.
| .-------. | | | |
| v | | '-----------------. |
Module------' | | | | .-------.
| | | | v |
| | Class---->(ClassModule)------'
| |
| |
| |
| | .-------.
| | v |
OtherClass-->(OtherModule)-----'


Which, assuming I haven't overlooked something vital, looks a whole lot more inviting.

Read: Shutting the Scary Door

Topic: Rubyist Magazine vol.8 Previous Topic   Next Topic Topic: Some Call It Schwag

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use