# Ensuring uniqueness of an object at creation time

**URL:** <https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763>\
**Category:** Ruby\
**Created:** [February 26, 2011, 6:45pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763 "2011-02-26T18:45:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 26, 2011, 6:45pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/1 "2011-02-26T18:45:46Z")

</div>

Hi all,

I would like to ensure that some attributes of an object are unique  
between  
all the instances of that class.  
So, I would like to prevent the creation of instances of a class that  
holds  
attributes “equal” to the attributes of an already created instance.

(solely) As an example, the problem of creating an instance of a Person  
class that has the same name of an existent instance.

My simple person class would be:

[http://pastie.org/1610255](http://pastie.org/1610255)

I found my way through overriding the Person.new method.  
(Thanks “The Ruby P.ming Language” book).  
So that the new instance is not even allocated if there’s already one  
with  
the same name.

[http://pastie.org/1610260](http://pastie.org/1610260)

QUESTIONS:

1. Could this be considered a poor design choice?
2. What other ways to accomplish this?

Any opinions, tips, critics are welcome.

Some more comments:  
Doing this way made me able to treat the object creation in a generic  
way.  
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 have a Hash that maps tags to classes.  
The uniqueness attribute requirements of each class is different.  
For example: Person unique by name. Telephone unique by number.  
The parser only:

1. Take the tag
2. Look for the class that is mapped by the tag
3. Create the class using the attributes and text as argument to new
4. The class is the “when” and “where” the uniqueness validation is in  
action

Thanks in advance,  
Abinoam Jr.

PS:  
My real problem is a little different (not Person or Telephone).  
I used the Person class just as an example.  
If someone curious about the code, tell me that I post the whole code.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 10:14am UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/2 "2011-02-27T10:14:11Z")

</div>

Abinoam Jr. wrote in post #984123:

> I found my way through overriding the Person.new method.  
> (Thanks “The Ruby P.ming 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.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 9:03pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/3 "2011-02-27T21:03:21Z")

</div>

Hi Brian,

Thank you very much for replying.

On Sun, Feb 27, 2011 at 5:14 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Abinoam Jr. wrote in post #984123:
> 
> > I found my way through overriding the Person.new method.  
> > (Thanks “The Ruby P.ming 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.

Person#initialize is called by Person.new  
The Person.allocate is called _before_ Person#initialize.

So, if I raise an exception at initialize point the object was already  
allocated.

> 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.

I think I was not clear enough. (I tried to simplify it, and ended  
OVERsimplifying it).

Look at this xml snippet.

 PREPARE\_A COMPLETE\_A

```
<message>PREPARE_B</message>
<message>COMPLETE_B</message>

<message>PREPARE_C</message>
<message>COMPLETE_C</message>

```

After declaring all those messages, I just want to use them in my  
rule/action table.

```
active
```
 PREPARE\_B completing 

Look at the \<send\_message\>PREPARE\_B\</send\_message\>  
This “PREPARE\_B” message is the same of the previously declared one.  
I’m just “using” it.  
In this specific case of its “uniqueness” is based on message  
text.  
(There’s other classes that has its uniqueness based on something  
different)  
“If it smells like dog, it should be a dog” (Or “THAT specific” dog).  
If it’s a message and has the same text, it should be the SAME message  
(not a new one).

So, I don’t want to raise an exception, I just want to return the  
existing object without even allocating a new one.

This kind of behavior makes me able to design my parser in a  
“generic”/“agnostic” manner.  
I just have to have a ‘table’ mapping xml tags to classes.  
The parser just get the tag, see what class should be instantiated,  
and calls the .new and iterate to the next tag.  
It’s up to the class all the logic to ensure its uniqueness.

But, I’m feeling I’m forgetting something.

What do you think?

a = :prepare\_b  
b = :prepare\_b

With Symbol, if it has the same value, it’s the SAME Symbol, not  
different ones.

a = “prepare\_b”  
b = “prepare\_b”

With String, even if they have the same value, they are different  
objects.

I would like to resemble/extend this kind of behaviour to more generic  
objects.

Thank you again,  
Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 9:30pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/4 "2011-02-27T21:30:56Z")

</div>

Hi Gary,

It’s pretty elegant using a name such as “find\_or\_create” for the  
factory method. It really describes its behavior much better than  
“new”.  
And setting “new” as private, good point too.

In my specific piece of software I just have to make all the class a  
“find\_or\_create” method, even if it doesn’t have any uniqueness  
constraints. This is because my parser is “agnostic”, so I don’t want  
it to decide where to use “new” or “find\_or\_create”. It has just to  
use “find\_or\_create” in every object it tries to create.

Thank you for your comments,  
Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 9:14pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/5 "2011-02-27T21:14:38Z")

</div>

On Feb 27, 2011, at 3:00 PM, Abinoam Jr. wrote:

> a = :prepare\_b  
> b = :prepare\_b
> 
> With Symbol, if it has the same value, it’s the SAME Symbol, not different ones.

Don’t get hung up on using the default new/initialize framework. You  
can define your own constructors that return existing instances instead  
of allocating a new instance if you want to model ‘value’ semantics.

message = Message.find\_or\_create(‘message\_a’, other, args)

Then define find\_or\_create such that it manages a cached collection of  
messages that it can dip into if it finds a match or it can create a new  
message if necessary.

In general I think it is better to define your own constructors if the  
‘normal’ semantics of new/initialize aren’t what you want instead of  
redefining new. You can always make new private if you want to ‘force’  
the use of your own constructors.

If the only attribute of your messages is their unique name, then  
symbols might be exactly what you need but be aware that symbols are  
generally not garbage collected so that if you create them based on  
external data you might be opening yourself to a memory exhaustion  
attack (i.e. the attacker can cause your memory footprint to grow  
without bounds). Whether this is a concern or not just depends on where  
the external data is coming from.

Gary W.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 10:52pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/6 "2011-02-27T22:52:14Z")

</div>

Abinoam Jr. wrote in post #984287:

> > There’s no need to do that. You could just raise an exception from  
> > within the initialize method.
> 
> Person#initialize is called by Person.new  
> The Person.allocate is called _before_ Person#initialize.
> 
> So, if I raise an exception at initialize point the object was already  
> allocated.

Yes, but it will be garbage-collected later.

> So, I don’t want to raise an exception, I just want to return the  
> existing object without even allocating a new one.

Then you could make a class method:

class MyClass  
@all\_objects = {}

# non-threadsafe version

def self.create(args)  
return @all\_objects[args] if @all\_objects[args]  
@all\_objects[args] = new(args)  
end  
end

> a = :prepare\_b  
> b = :prepare\_b
> 
> With Symbol, if it has the same value, it’s the SAME Symbol, not  
> different ones.
> 
> a = “prepare\_b”  
> b = “prepare\_b”
> 
> With String, even if they have the same value, they are different  
> objects.

That’s correct. And normal objects are like String; Symbol is very much  
a special case, baked into the language, to give efficient method  
dispatch.

Why is it important in your application for your objects to have the  
singleton behaviour like Symbol? What bad things would happen if there  
were two objects representing the same message?

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 11:55pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/7 "2011-02-27T23:55:09Z")

</div>

> One option:
> 
> class Object  
> def self.find\_or\_create(\*args,&blk)  
> new(\*args,&blk)  
> end  
> end
> 
> Then you override it in those classes where you need to.

Good! Thank you.

Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 27, 2011, 10:53pm UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/8 "2011-02-27T22:53:31Z")

</div>

Abinoam Jr. wrote in post #984295:

> In my specific piece of software I just have to make all the class a  
> “find\_or\_create” method, even if it doesn’t have any uniqueness  
> constraints. This is because my parser is “agnostic”, so I don’t want  
> it to decide where to use “new” or “find\_or\_create”. It has just to  
> use “find\_or\_create” in every object it tries to create.

One option:

class Object  
def self.find\_or\_create(\*args,&blk)  
new(\*args,&blk)  
end  
end

Then you override it in those classes where you need to.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 28, 2011, 12:03am UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/9 "2011-02-28T00:03:18Z")

</div>

> > So, if I raise an exception at initialize point the object was already  
> > allocated.
> 
> Yes, but it will be garbage-collected later.

You’re right. In my specific piece of software I think this will not a  
problem.  
But, if it’s a huge one, the computational cost of allocating and  
deallocating can be important.

> # non-threadsafe version

Thank you for advising me to always think in “thread-safe” way.

> Why is it important in your application for your objects to have the  
> singleton behaviour like Symbol? What bad things would happen if there  
> were two objects representing the same message?

Look, there’s some problems I have circumvented by defining a (class)#==  
method.  
The “==” method relied on the comparison of the instance variables (  
@message, for example ).  
So, even if there are 2 instances representing the same message they  
would be considered equal to each other throughout the software.  
But… again, I thought it could a little ugly code.

Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![abinoampraxedes\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@abinoampraxedes\_m](https://www.ruby-forum.com/u/abinoampraxedes_m)\
**Post date:** [February 28, 2011, 12:13am UTC](https://www.ruby-forum.com/t/ensuring-uniqueness-of-an-object-at-creation-time/203763/10 "2011-02-28T00:13:19Z")

</div>

On Sun, Feb 27, 2011 at 6:02 PM, Abinoam Jr. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You’re right. In my specific piece of software I think this will not a problem.  
> But, if it’s a huge one, the computational cost of allocating and  
> deallocating can be important.

And you’ve already proven that allocation is a significant factor in  
your program’s runtime efficiency using rigorous profiling… right?
