Get an object from the encoding reference

4 views
Skip to first unread message

Maese

unread,
Aug 25, 2011, 12:53:03 PM8/25/11
to Ruby on Rails: Talk
hi, i have a string with the enconding reference like "#<MyObject:
0x2b9027de>"
there any way to get the object from the string ?
Thanks

Tim Shaffer

unread,
Aug 25, 2011, 1:08:03 PM8/25/11
to rubyonra...@googlegroups.com
object_id = "0x2b9027de".to_i(16) / 2
object = ObjectSpace._id2ref(object_id)

Matt Jones

unread,
Aug 26, 2011, 10:55:58 AM8/26/11
to Ruby on Rails: Talk
Tim's method works - but you should REALLY REALLY think about what's
causing you to want to do this. This sort of trick could be handy when
debugging, but using it in code that's meant to run reliably can wind
up giving you some serious headaches. For instance, the object
referred to by the string could get garbage-collected before you can
dereference it...

--Matt Jones
Reply all
Reply to author
Forward
0 new messages