[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class inheriting from class within the namespace

Gavin Kistner

10/31/2006 4:41:00 PM

Tree structures are interesting. Often the root of the tree is used as a
handle for the tree itself. However, the tree often has additional
methods beyond that of the root node.

I couldn't figure out how to do this simply via inheritance due to
chicken-and-egg problems with namespacing:

class Tree < Tree::Node
class Node
end
end
#=> chicken-and-egg.rb:1: uninitialized constant Tree (NameError)


I came up with two alternative techniques to achieve the same goal. I'm
not wild about the way either are handled by RDoc (though the first is
substantially better):

# Technique 1: Using modules
class Tree
module NodeStuff
#...all the meat here...
end
include NodeStuff

class Node
include NodeStuff
end
end

# Technique 2: Conjur the chicken first, and kill it later
class TmpTreeNode
# ...all the meat here...
end
class Tree < TmpTreeNode
Node = TmpTreeNode
end
Object.class_eval{ remove_const( :TmpTreeNode ) }


Ideally, IMO, Ruby would let me do this:
# No "Tree" module or class has yet been defined;
# it is a pure namespace at this point
class Tree::Node
# ...all the meat here...
end

# Now Tree is a class
class Tree < Tree::Node
end


I don't have any question, and it's not important enough to me to make
an RCR for the above. I just thought I'd share these thoughts with the
world.

3 Answers

matt

10/31/2006 5:20:00 PM

0

Gavin Kistner <gavin.kistner@anark.com> wrote:

> Tree structures are interesting. Often the root of the tree is used as a
> handle for the tree itself. However, the tree often has additional
> methods beyond that of the root node.
>
> I couldn't figure out how to do this simply via inheritance due to
> chicken-and-egg problems with namespacing:
>
> class Tree < Tree::Node
> class Node
> end
> end
> #=> chicken-and-egg.rb:1: uninitialized constant Tree (NameError)

Why wouldn't you do this?

module TreeStuff
class Node
end
class Tree < Node
end
end

A tree does more than a node (you say), so a tree is a subclass of node
("a node and then some"). The pair are wrapped up in a module to isolate
their namespace.

Of course I could be missing some other desideratum. m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Jack Linthicum

11/21/2008 9:17:00 PM

0

On Nov 21, 3:51 pm, Jack Linthicum <jacklinthi...@earthlink.net>
wrote:
> On Nov 21, 3:11 pm, Les Cargill <lcarg...@cfl.rr.com> wrote:
>
>
>
> > Dan wrote:
> > > Les Cargill wrote:
> > >> J Antero wrote:
> > >>> I have a feeling that if the Detroit automakers wind up in Cahpter 7
> > >>> bankruptcy (doors closed, not just reorganization), that there will
> > >>> be a couple pages in Econ 101 textbooks 50 years from now, discussing
> > >>> what a very big mistake it was..
>
> > >>> The current financial crisis has a certain "superficial wounds"
> > >>> aspect to it.
>
> > >>> Losing Detroit automakers is more like a gut wound.
>
> > >> They've been putting lipstick on that corpse for twenty years. Maybe
> > >> longer.
>
> > >> I do have to wonder just how much CAFE standards are to blame, though
> > >> - not that Detroit handled them very well, but still....
>
> > > Lack of them in the last administration hurt Detroit enormously.
>
> > > Dan
>
> > But the end consumer simply purchased around CAFE standards by
> > buying SUVs. It's anything but clear that that "hurt" anybody
> > (discounting environmental damage).
>
> > --
> > Les Cargill

Correcting some gaffs.
>
> SUVs have more roll-overs, which are fatal 80% of the time when driver
> isn't wearing seat belt. There were 10k deaths in 2007 due to rollovers, which I think makes them something like 20-25% of all highway deaths. They also are high in back up
> accidents, and , because of their usually greater weight (and
> therefore mass) they do more damage than ordinary cars when a crash
> occurs. Also without air bags the occupants suffer more harm in side
> collisions, despite the higher seat. SUVs are classified as light
> trucks, some more of the Big 3 fictions to pass even the low CAFE
> standards. As such their bumpers do not have the "same height" rule as
> passenger cars. Try googling SUV accident rate.
>
> Personal observation says the driver of an SUV will be a small woman
> with children, usually not in restraints, who is talking on a phone.
>
> http://www.suv-rollovers.com...

Jack Linthicum

11/22/2008 12:31:00 AM

0

On Nov 21, 6:47 pm, "John Briggs" <john.brig...@ntlworld.com> wrote:
> "Jack Linthicum"  wrote
>
>
>
> > Correcting some gaffs.
>
> gaffes.
>
> >> SUVs have more roll-overs, which are fatal 80% of the time when driver
> >> isn't wearing seat belt. There were 10k deaths in 2007 due to rollovers,
> >> which I think makes them something like 20-25% of all highway deaths.
> >> They also are high in back up accidents, and , because of their usually
> >> greater weight (and therefore mass) they do more damage than ordinary
> >> cars when a crash occurs.
>
> How (roughly) are you distinguishing between weight and mass?
> --
> John Briggs

If you print it in the manual it's weight, if you measure it in an
impact test it's mass.