On 14 Mar 2000, Dave Thomas wrote: > Hugh Sasse Staff Elec Eng <hgs / dmu.ac.uk> writes: > > > I asked someone about how to do this neatly on an OO way, and they > > said, "have the ports contain references to the voltages". However, > > I cannot see any reference or pointer types in ruby. Is there a > > neat way of doing this in ruby? > > In Ruby, variables _are_ references to objects, so you could implement > your port with (say) an array of voltages, and the node as an array of > ports. > > Say you have > > v1 = "Fred" > v2 = v2 Ah! looking at the FAQ again, this is 2.1 and 7.4. > > Then in terms of memory and references, what you've done is assigned a > reference to the String "Fred" to v2, and then copied that reference > to v2. > > > v1 --------- > \ __String____ > -->| | > -->| Fred | > / |____________| > v2 --------- > > (sorry, I missed the ASCII art class at school) That is vary clear. Thank you. So when I create my ports within my node, I can pass in a pointer to the node... class Port def new(parent, portnum) initialise(parent, portnum) end def initialize(parent, portnum) @parent = parent case portnum when 1 @v1 = @parent.vxy1 @v2 = @parent.vxz1 when 2 @v1 = @parent.vyx1 @v2 = @parent.vyz1 > # etc end end class Node def new() ... end def initialize() # setup the voltages... @vxy1 = 0.0 attr("vxy1", true) ... # setup the ports passing myself as parent... port1 = Port.new(self,1) port2 = Port.new(self,2) #etc end end sort of thing? I'm just a bit unsure about the instance variables in both types -- I know I can make them public with attr() but that creates *functions* to access them[?], so will that mean that the port cannot point to the parent's variables, or not? I will have several nodes and several ports, so it must be the case that the voltages (for a node or a port) are not shared across all instances of the same type (node or port). Hence my question about instance variables. Thank you, > Regards > > > Dave > Hugh hgs / dmu.ac.uk