From: Tobias DiPasquale Date: 2001-12-05T16:12:52+09:00 Subject: [ruby-talk:27527] re: dynamic class loading? Hi, Sorry, the previous code tested fine for me, but didn't actually work for what I wanted it for. Below is some updated code, in case anyone wants it. The result of the 'p @klasses' line is because I read in three files which contained classes 'A', 'B', and 'C', and a module 'X'. Does anyone know of a better way to do this? [localhost]% cat example.rb @klasses = {} @log = # ==> singleton logging service object @props = # ==> hash; created from a file of properties (all Strings) def loadObjects base = {} klass_spec = @props['container.klass_spec'] raise PropertyException unless FileTest.exist?(klass_spec) # get list of classes already loaded ObjectSpace.each_object(Class) { |aClass| base[aClass.to_s] = aClass.id } IO.foreach(klass_spec) { |line| line.chomp! begin ret = require line rescue @log.logError "error loading " + line + ": " + $! ensure @log.logInfo "loaded " + line unless ret == nil end } # now get the classes that we just loaded in ObjectSpace.each_object(Class) { |aClass| @klasses[aClass.to_s] = aClass.id if base[aClass.to_s] == nil } end #loadObjects p @klasses [localhost]% ruby example.rb {"X"=>537844792, "A"=>537846942, "B"=>537845252, "C"=>537845362} [localhost]% -- 0100001101000010010000110100011101110101 << Tobias DiPasquale >> -=[ Solaris Systems Administrator ]=- Villanova University ECE Dept. mailto: anany@ece.vill.edu -=[ Applications Engineering Consultant ]=- BCS Solutions, Inc. mailto: tdipasquale@bcssinc.com 0100001101000010010000110100011101110101