[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

retrieving an object by it's id

Thomas Hafner

1/28/2007 5:08:00 PM

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
4 Answers

Aaron Patterson

1/28/2007 6:07:00 PM

0

On Mon, Jan 29, 2007 at 02:15:11AM +0900, Thomas Hafner 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

--
Aaron Patterson
http://tenderlovem...

Thomas Hafner

1/28/2007 6:19:00 PM

0

Aaron Patterson <aaron_patterson@speakeasy.net> wrote/schrieb <20070128190516.GA5600@eviladmins.lan>:

> 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

Robert Klemme

1/28/2007 6:51:00 PM

0

On 28.01.2007 19:18, Thomas Hafner wrote:
> Aaron Patterson <aaron_patterson@speakeasy.net> wrote/schrieb <20070128190516.GA5600@eviladmins.lan>:
>
>> 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.

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

Kickin' Ass and Takin' Names

1/20/2012 4:14:00 AM

0

Bible Studies with Satan <bible@hotmail.com> wrote in
news:GP2dndaNSO90F4XSnZ2dnUVZ_uCdnZ2d@mchsi.com:

> Kickin' Ass and Takin' Names wrote:
>
>>
>>
>>
>> One evangelical leader says that Republican presidential candidate
>> Newt Gingrich is like the Italian captain whose cruise ship sank ?
>> because the former House Speaker was sailing on his own personal love
>> boat when he cheated on his first and second wives.
>
> Conservatives float in their ocean of vitriol. I love how the vitriol
is
> spilling over into nasty attacks on each other.
>
> Ronald Reagan's 11th commandment for Republicans is history!

I loved our weekend together!