# Retrieving an object by it's id

**URL:** <https://www.ruby-forum.com/t/retrieving-an-object-by-its-id/83670>\
**Category:** Ruby\
**Created:** [January 28, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/retrieving-an-object-by-its-id/83670 "2007-01-28T18:16:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [January 28, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/retrieving-an-object-by-its-id/83670/1 "2007-01-28T18:16:14Z")

</div>

Hello,

how can I write a method f which fullfills the following  
specification?

The function f has one Fixnum argument oi.  
f(oi) == [true, obj], if there’s an object obj with obj.object\_id == oi;  
f(oi) == [false, nil], otherwise.

Regards  
Thomas

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [January 28, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/retrieving-an-object-by-its-id/83670/2 "2007-01-28T19:07:29Z")

</div>

On Mon, Jan 29, 2007 at 02:15:11AM +0900, Thomas H. wrote:

> Hello,
> 
> how can I write a method f which fullfills the following  
> specification?
> 
> The function f has one Fixnum argument oi.  
> f(oi) == [true, obj], if there’s an object obj with obj.object\_id == oi;  
> f(oi) == [false, nil], otherwise.

def f(oi)  
g = [false, nil]  
ObjectSpace.each\_object { |a|  
g = [true, a] if a.object\_id == oi  
}  
g  
end

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [January 28, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/retrieving-an-object-by-its-id/83670/3 "2007-01-28T19:25:22Z")

</div>

Aaron P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote/schrieb  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> def f(oi)  
> g = [false, nil]  
> ObjectSpace.each\_object { |a|  
> g = [true, a] if a.object\_id == oi  
> }  
> g  
> end

Thanks, but:  
4.object\_id  
=\> 9  
f(9)  
=\> [false, nil]  
I’d expect [true,4], instead.

Regards  
Thomas

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [January 28, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/retrieving-an-object-by-its-id/83670/4 "2007-01-28T19:55:26Z")

</div>

On 28.01.2007 19:18, Thomas H. wrote:

> Thanks, but:  
> 4.object\_id  
> =\> 9  
> f(9)  
> =\> [false, nil]  
> I’d expect [true,4], instead.

You don’t want to use #each\_object for this - this is way to  
inefficient. You want #\_id2ref:

irb(main):006:0\> ObjectSpace.\_id2ref 4.object\_id  
=\> 4  
irb(main):007:0\> ObjectSpace.\_id2ref “foo”.object\_id  
=\> “foo”

Kind regards

robert
