# Challenge for object\_id, Garbage Value

**URL:** <https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633>\
**Category:** Rails\
**Created:** [August 12, 2009, 11:56am UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633 "2009-08-12T11:56:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [August 12, 2009, 11:56am UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633/1 "2009-08-12T11:56:35Z")

</div>

I am facing strange problem.

I have an object @part=MainPart.find(:all)

1. @part.each\_with\_index do |mainpart,index|

2. \<%= mainpart.object\_id%\>

3. end

object\_id field in my table . Now i want to get value \<%=  
mainpart.object\_id%\> is giving GARBAGE VALUE. Is there any solution  
apart from removing/change object\_id field  
from table .

Thanks,  
Angela .

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [August 12, 2009, 12:59pm UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633/2 "2009-08-12T12:59:46Z")

</div>

On Aug 12, 10:56 am, Ruby on Rails [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am facing strange problem.
> 
> I have an object @part=MainPart.find(:all)
> 
> 1. @part.each\_with\_index do |mainpart,index|
> 
> 2. \<%= mainpart.object\_id%\>
> 
> 3. end

It’s not just reserved by rails, it is a fundamental ruby method. You  
can get the attribute value with read\_attribute or [] (which are the  
same thing. You could add your own object\_id accessor but I’m not sure  
whether you would be storing up trouble for later ( object\_id is also  
available as **id** but other code might not be expecting object\_id to  
have been overridden)

Fred

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [August 13, 2009, 4:25am UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633/3 "2009-08-13T04:25:41Z")

</div>

Thanks Fred ,

I am not getting that concept of ruby . We have nil.object\_id is 4  
,are they fix for every value, i am not sure for this .By the way thanks  
for this.It’s working now.

Thanks Again Fred…

Have nice time .

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [August 13, 2009, 6:43pm UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633/4 "2009-08-13T18:43:32Z")

</div>

On Wed, Aug 12, 2009 at 10:25 PM, Ruby on  
Rails[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred ,
> 
> I am not getting that concept of ruby . We have nil.object\_id is 4  
> ,are they fix for every value, i am not sure for this .

No, not in general, and the real story is rather dependent on the  
particular Ruby implementation.

If we are talking the MRI (or Matz’ Ruby Implementation) then there  
are a few objects like nil, true, false… with fixed object\_ids, also  
FixedNum instances have an object\_id which can be computed from their  
value.

In general though the object\_id is typically related to the address of  
the object in memory, so you can’t rely on any fixed mapping based on  
value, in fact, the object\_id which is the identity of the object has  
to be independent of the state for a mutable object.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [September 14, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633/5 "2009-09-14T21:39:34Z")

</div>

On Mon, Sep 14, 2009 at 1:39 PM, rkj2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Does that mean lets say on a deployed app, the Rails server/stack  
> starting initiates new object\_id’s being assigned, are the object\_id’s  
> still unique for each object over time (with server restarts etc.) if  
> they are based on a time algorithm ?

No, except for a few objects (and which objects is dependent on the  
particularly Ruby implementation, there are no guarantees about the  
uniqueness of object\_ids over time or even between two daemon  
processes running at the same time on the same machine.

It’s even conceivable that some implementations might change the id of  
an object over time due to the implementation of the garbage  
collector, athough I’m not aware of any current Ruby implementations  
which do so.

In Ruby, as in life, the concept of identity is more of a  
philosophical one than can be relied upon

> **[Ship of Theseus](https://en.wikipedia.org/wiki/Ship_of_Theseus)**
>
> The Ship of Theseus is a thought experiment about whether an object which has had all of its original components replaced remains the same object. According to legend, Theseus, the mythical Greek founder-king of Athens, rescued the children of Athens from King Minos after slaying the minotaur and then escaped onto a ship going to Delos. Each year, the Athenians commemorated this by taking the ship on a pilgrimage to Delos to honor Apollo. A question was raised by ancient philosophers: After seve...

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![dharshannew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/5fc32e/32.png) [@dharshannew](https://www.ruby-forum.com/u/dharshannew)\
**Post date:** [September 14, 2009, 7:41pm UTC](https://www.ruby-forum.com/t/challenge-for-object-id-garbage-value/173633/6 "2009-09-14T19:41:17Z")

</div>

Hi,

Does that mean lets say on a deployed app, the Rails server/stack  
starting initiates new object\_id’s being assigned, are the object\_id’s  
still unique for each object over time (with server restarts etc.) if  
they are based on a time algorithm ?

Thanks for your help and clarification on this.

Rajive
