From: "Paatsch, Bernd" Date: 2006-02-09T03:03:06+09:00 Subject: Why do I have three objects instead of one? ------_=_NextPart_001_01C62CD9.C4046608 Content-Type: text/plain Hello, I run into a problem with instances of classes I created. Here is the scope what I want to do: I like to have a class that currently contains out of an Array that itself contains instances of other classes called TestMemory (I used C in the past which would mean that the array would contain pointers., pointing to the object I like to reference). Printing out the Array I would expect obj1, obj2, ... However I get obj1, obj1, obj1, obj2, obj2, obj2, obj2, obj2, obj2 ...., which correlates with the input date I read into the object. Could anybody tell me why this is and what I am doing wrong. It might be that I haven't grasp how Ruby references objects. Result when I run the code (which is at the end of my code snipe): decission.hashTC.each do |key, value| puts key + " => " + value end (I would only expect one Login object not three!) # security => 0000 user => 9208 language => United States English # security => 0000 user => 9208 language => United States English # Here is the code: class TestMemory # container that should hold all other classes attr_reader :listTM @@aName = "#{self.name}".sub("#{$moduleName}::","") def initialize() @listTM = Array.new() end # adds a test step to the list def append( testStep ) @listTM.push( testStep ) end # deleting the first entry from list, while returning it def delete_first @listTM.shift end end # class TestMemory class Login attr_reader :hashTC @@aName ="#{self.name}".sub("#{MODULENAME}::","") def initialize @hashTC = Hash.new end def name @@aName end def populate(input) tempvalue = input.split(":") if tempvalue != nil case tempvalue[0] when "user" : hashTC["user"] = tempvalue[1]; when "security" : hashTC["security"] = tempvalue[1] when "language" : hashTC["language"] = tempvalue[1] end end end end end # class TestCase ###################################### ## variables ###################################### clsTestCase = nil clsTestMemory = nil clsLogin = nil clsAddSubscriber = nil strFile = "sample.csv" aryInput = nil groupSignal = nil ###################################### ## Initialize ###################################### clsTestMemory = TestMemory.new ###################################### ## Read File ## read everything at once into an array ## and then process it ###################################### File.open(strFile, "r") do |file| # read in all lines into an array aryInput = file.readlines length = aryInput.length end # go through array, read first element and delete it out of the array while 0 != aryInput.length firstElement = aryInput.at(0) # nextElement = aryInput.at(1) aryInput.delete_at(0) # remove newline characters inputCase = firstElement.chomp # based of the structure of the input file #Testcase #--web:wmm #--baseurl:172.16.19.90 #Login #--verify:userid:User ID #it has indicators followed by the action unless inputCase.include? "--" then case inputCase.upcase when "TestCase".upcase if nil == clsTestCase then clsTestCase = TestCase.new groupSignal = inputCase.upcase end next when "login".upcase if nil == clsLogin then clsLogin = Login.new groupSignal = inputCase.upcase end next when "AddSubscriber".upcase if nil == clsAddSubscriber then clsAddSubscriber = SubAddSubscriber.new groupSignal = inputCase.upcase end next end end # as long as the same group read into the same class-structure case groupSignal when "TestCase".upcase if inputCase.include? "--" then clsTestCase.populate( inputCase.delete("--") ) else groupSignal = nil end # append_hash( inputCase, clsTestCase ) next when "login".upcase append_hash( inputCase, clsLogin ) clsTestMemory.append( clsLogin ) next when "addSubscriber".upcase append_hash( inputCase, clsAddSubscriber ) clsTestMemory.append( clsAddSubscriber ) next else puts inputCase end end ###################################### ## Test to see if it works ###################################### decission = nil decission = clsTestMemory.delete_first while decission puts decission decission = clsTestMemory.delete_first decission.hashTC.each do |key, value| puts key + " => " + value end end --Bernd ------_=_NextPart_001_01C62CD9.C4046608--