From: Tobias DiPasquale Date: 2001-12-05T11:42:26+09:00 Subject: [ruby-talk:27510] dynamic class loading? Hi all, As a sort of an aside to my last post about how to load classes on demand with 'require', I was wondering if there was a good way to figure out the names of the classes that I just loaded in from file(s). Here is my weak solution: ========================================== # to hold the class names that are already loaded base_array = [] # get list of classes already resident ObjectSpace.each_object(Class) { |aClass| base_array << aClass.inspect } # load 'em up, move 'em out, ride 'em in, Rawhide! Yeeha! IO.foreach(klass_spec) { |line| line.chomp! # to snap off newline characters; thanks Dave! begin ret = require line rescue LoadError @log.logError "error loading " + line + ": " + $! ensure @log.logInfo "loaded " + line unless ret == nil end } # now get just the classes we just loaded ObjectSpace.each_object(Class) { |aClass| @klasses << aClass.inspect unless base_array.include?(aClass) } =========================================== The code reads in filenames, but as all of you already know, there can be multiple classes/modules in a file, and the filename doesn't have to have anything to do with the classes/modules inside the file. Any way to make this code more elegant/reliable? This just seems to me to be too much of a kludge to be the "Ruby way". -- 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