From: Brian Candler Date: 2011-02-27T18:14:13+09:00 Subject: Re: Ensuring uniqueness of an object at creation time Abinoam Jr. wrote in post #984123: > I found my way through overriding the Person.new method. > (Thanks "The Ruby Programming Language" book). > So that the new instance is not even allocated if there's already one > with > the same name. There's no need to do that. You could just raise an exception from within the initialize method. > The logic behind the uniqueness of the instances is hold by the class > itself, not by the (running) code. > This is desirable for me in this specific set because I'm parsing an xml > file with tags in a recursive manner. I think it's a poor design choice to enforce uniqueness within the class, because it limits the usefulness of your Person class - you could not have two XML parsers parsing two separate documents, for example, or send and receive Person instances using DRb. I think it would be better to have a 'person collection' object which enforces the uniqueness. You create a new person, and get an error if you try to add it into the collection where one already exists. This is the same sort of model as you get with SQL uniqueness constraints within a table, of course. Regards, Brian. -- Posted via http://www.ruby-forum.com/.