# \_id2ref in finalize or how get object.var from class.finalize

**URL:** <https://www.ruby-forum.com/t/id2ref-in-finalize-or-how-get-object-var-from-class-finalize/123274>\
**Category:** Ruby\
**Created:** [December 15, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/id2ref-in-finalize-or-how-get-object-var-from-class-finalize/123274 "2007-12-15T19:54:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrey\_Nikitin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@Andrey\_Nikitin](https://www.ruby-forum.com/u/Andrey_Nikitin)\
**Post date:** [December 15, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/id2ref-in-finalize-or-how-get-object-var-from-class-finalize/123274/1 "2007-12-15T19:54:31Z")

</div>

Hi, all.

#!/usr/bin/ruby1.8

include ObjectSpace

class MyClass  
attr\_reader :var  
def initialize(var)  
@var = var  
puts “from initialize: #{@var}”  
define\_finalizer(self, self.class.method(:finalize).to\_proc)  
end

def MyClass.finalize(object\_id)  
o = \_id2ref(object\_id)  
puts “from finalize: #{o.var}”  
end  
end # class Storage

o = MyClass.new(“The Value!”)  
exit 0

Why lines of a code after a call of function “\_id2ref(object\_id)” are  
never carried out?

How it is possible to get access to variables or methods of a object  
from a method of a class :finalize?

---

<div class="post-metadata">

**Author:** ![Andrey\_Nikitin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@Andrey\_Nikitin](https://www.ruby-forum.com/u/Andrey_Nikitin)\
**Post date:** [December 16, 2007, 4:47am UTC](https://www.ruby-forum.com/t/id2ref-in-finalize-or-how-get-object-var-from-class-finalize/123274/2 "2007-12-16T04:47:06Z")

</div>

Hi,

At Sun, 16 Dec 2007 03:53:49 +0900,  
Andrey Nikitin wrote in [ruby-talk:283702]:

> def MyClass.finalize(object\_id)  
> o = \_id2ref(object\_id)  
> puts “from finalize: #{o.var}”  
> end

Finalizer is called after the object was disposed. So \_id2ref  
raises an exception.

> How it is possible to get access to variables or methods of a object  
> from a method of a class :finalize?

It does not exist at that moment any more.
