From: Robert Klemme Date: 2005-03-22T16:49:51+09:00 Subject: Re: Loading singletons from a file. ------=_NextPart_000_001A_01C52EBB.6886BD70 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit "Travis Smith" schrieb im Newsbeitrag news:d95bacd905032121284d8f756b@mail.gmail.com... > Say I have a class Here, and I loaded that class from some file > outside my driver via require. So now the class Here is in my > namespace and I can create a new one with Here.new. What I would like > to do is create a singleton of the same class with information stored > in another ruby file. That file would contain something like this.... > > class Here > def initialize > @var1 = "something else" > end > > # adding a method that isn't part of the normal Here class > def new_method > # do something > end > end > > Now, I could just create the singleton and store it somewhere for when > I needed it, but I plan on having a number of these. Each of them in > their own file. I thought perhaps I would need to add a line of code > to the end of the to store that singleton where I needed it. This > really didn't end up working for me. It would just use the global > class when I would load the file via load _filename_, false. > > What I would really like is just some way to turn a file into an > anonymous class and be able to initialize it just as is. I'm not sure > if that's easily possible though. Have a look at the attached files and see whether that helps you. > Thanks for any ideas here. I've only been on Ruby for four days now, > but it's been pretty easy to get this far. :-) Kind regards robert ------=_NextPart_000_001A_01C52EBB.6886BD70 Content-Type: application/octet-stream; name="here.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="here.rb" =0A= class Here=0A= def self.instances() @instances ||=3D {} end=0A= end=0A= =0A= ------=_NextPart_000_001A_01C52EBB.6886BD70 Content-Type: application/octet-stream; name="here-main.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="here-main.rb" require 'here-1'=0A= =0A= p Here.instances=0A= ------=_NextPart_000_001A_01C52EBB.6886BD70 Content-Type: application/octet-stream; name="here-1.rb" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="here-1.rb" =0A= require 'here'=0A= =0A= tmp =3D Here.new=0A= =0A= class <