From: Robert Klemme Date: 2010-06-21T06:00:06+09:00 Subject: Re: General Confusion On 20.06.2010 20:20, Chuck Brotman wrote: > This question may be more about OOP ion general than Ruby, but since I'm > implementing in Ruby (trying to, anyway) I thought I'd start here. If I > should be posting elsewhere please let me know. The place is perfectly OK. > Here's the problem: > I'm trying to write my own graph theory library. I'm aware of > Adjacency matrices and the like, but I want to do this as OO as > possible, using objects of class node and edge to implement. > > Each node has a name and an array of adjacent nodes. How can I add a > reference to a node which is adjacent,if I haven't created that node > yet? Or, do I have to create all the nodes first (without adjacencies > and then back fill them?) Or?? You need to have the node created before you add it if you want to keep direct references. I'd say that is pretty much straightforward. Note that you do not need to create *all* nodes before you start creating edges. If you implement your graph in a way so nodes have ids (say integers) you could use those for referencing other nodes. But then you also need some mechanism to record which nodes are there, i.e. you could create another class Graph which holds that information. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/