Class inheriting from class within the namespace

Class inheriting from class within the namespace

Post by Gavin Kist » Thu, 02 Nov 2006 01:40:42


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.
 
 
 

Class inheriting from class within the namespace

Post by matt » Thu, 02 Nov 2006 02:19:38


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 = XXXX@XXXXX.COM , http://www.yqcomputer.com/
Tiger - http://www.yqcomputer.com/
AppleScript - http://www.yqcomputer.com/
Read TidBITS! It's free and smart. http://www.yqcomputer.com/

 
 
 

Class inheriting from class within the namespace

Post by Gavin Kist » Thu, 02 Nov 2006 02:44:15

From: matt neuburg [mailto: XXXX@XXXXX.COM ]

Only because it's an annoying namespace level that the user would need
to either repeatedly specify or include. If I wrap all my own classes in
a Phrogz namespace, then the user needs to type:
tree = Phrogz::TreeStuff::Tree.new
and that's a little ugly IMHO. If a class can serve as its own
namespace, I think that it makes sense, to let it do so.

I also prefer to place subordinate/helper classes inside the class that
they augment. To my way of thinking, it makes little sense to use a
TreeStuff::Node outside of the context of a TreeStuff::Tree. As such, I
would prefer to see that implicit dependency suggested by letting the
Tree wrap its big old arms around the Node and say "MINE!".

But you raise a good point; that's certainly a good, simple way to
achieve the goal without spewing classes about willy-nilly.