From: Thomas Adam Date: 2004-03-24T01:09:11+09:00 Subject: Re: How to avoid duplicate object creations? --- Stig Sandbeck Mathisen wrote: > > I have a program that will create a lot of objects, mainly of type > SomeDocumentation::Host and SomeDocumentation::Role. > > Since the Role objects typically create a lot of Host objects, and the > other way around, I tried to be clever, and look up objects that > already exists with the same value of "name", since the initialize > reads a bit from disk. > > I've tried tried to solve it with the following code snippet: > > def initialize(name) > > ObjectSpace.each_object(self.class) do |anObject| > return anObject if anObject.name == name > end > > the same .name attribute/method/whatever yet> > > end You might be able to use the "freeze" method from the Object class. -- Thomas Adam ===== "The Linux Weekend Mechanic" -- http://linuxgazette.net "TAG Editor" -- http://linuxgazette.net " We'll just save up your sins, Thomas, and punish you for all of them at once when you get better. The experience will probably kill you. :)" -- Benjamin A. Okopnik (Linux Gazette Technical Editor) ___________________________________________________________ Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now http://uk.messenger.yahoo.com/download/index.html