From: Tobias DiPasquale Date: 2001-12-03T14:57:59+09:00 Subject: [ruby-talk:27322] HELP!! loading multiple classes from a file??? Hi all, I really need the community's help on this one. Is it possible to load multiple classes after a Ruby program has started? Here is what I am trying to do: I need to load an arbitrary number of classes from their respective files after I start a Ruby program, where other objects have already been instantiated. Please note the results of the following little test program: % pwd /home/anany/RubiCTM % ls -l total 88 -rw-rw-r-- 1 anany anany 50 Dec 3 00:36 a.rb -rw-rw-r-- 1 anany anany 49 Dec 3 00:37 b.rb -rw-rw-r-- 1 anany anany 49 Dec 3 00:37 c.rb -rw-rw-r-- 1 anany anany 75 Dec 3 00:28 klass_spec.conf -rw-rw-r-- 1 anany anany 305 Dec 3 00:37 propogate.rb ...remaining output removed for brevity % cat klass_spec.conf /home/anany/RubiCTM/a.rb /home/anany/RubiCTM/b.rb /home/anany/RubiCTM/c.rb % cat a.rb b.rb c.rb class A def initialize @value = "a" end end class B def initialize @value = "b" end end class C def initialize @value = "c" end end % cat propogate.rb klass_file = "klass_spec.conf" i = 0 a = [] IO.foreach(klass_file) { |line| # load each file and resolve classes begin ret = require line rescue LoadError puts "error reading: " + line + ": " + $! ensure puts "return value: " + ret unless ret == nil end # for now, just keep track of those successfully loaded if ret i += 1 a[i] = line end } puts "Array ==> [" + a.to_s + "]" % ruby propogate.rb error reading: /home/anany/RubiCTM/a.rb : No such file to load -- /home/anany/RubiCTM/a.rb error reading: /home/anany/RubiCTM/b.rb : No such file to load -- /home/anany/RubiCTM/b.rb error reading: /home/anany/RubiCTM/c.rb : No such file to load -- /home/anany/RubiCTM/c.rb Array ==> [] If it makes a difference, here are my machine specs: OS: RedHat 7.2 (kernel 2.4.7-10) CPU/speed: Pentium III/800 MHz ruby --version: 1.6.5 ruby ./configure options: none The goal of this is to be able to have requests come in from a dRuby naming service (from client --> naming service --> object container --> client) and have the container, who has already loaded the class definitions for the objects requested, just instantiate one of the objects and return it to the client. I was trying to find Marshal#load's source code (in Ruby), but it appears to be in libruby.so. (no help there) Likewise, Kernel::load is unsuitable as load _executes_ a file, whereas all I want to do is load a class(es) from a file. Kernel::require seems to be my only hope, but even this is erroring out on a simple (?) task like the above. Anybody know why the above code doesn't work, or how to make it work? -- 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