[ruby-it] Distruttore di classi

Michele Casari lablinux a gmail.com
Mar 16 Set 2008 11:52:31 CEST


visto, funziona, me c'è un problema...
le variabili di istanza non riesco a gestirle.
Prova questo:
class MyClass
    attr_reader :pippo
    def initialize(pippo)
        @pippo = pippo
        ObjectSpace.define_finalizer(self,self.class.method(:finalize).to_proc)
    end

    def self.finalize(id)
        puts "Object #{id} dying at #{Time.new}"
        puts "qui 1"
        puts "init close : " + @pippo
        puts "qui 2"

    end
end

# test code
a = MyClass.new('a')
a = nil
puts 'FUORI DALL\'OGGETTO'
puts ''

ObjectSpace.garbage_collect

visualizza le prime due puts (di finalize) la terza no.

Ciao Michele.
-- 
Posted via http://www.ruby-forum.com/.


More information about the Ml mailing list