4

I'm using RubyPython to import a Python module. I am doing RubyPython.start in the constructor (initialize), and I suppose I should symmetrically do RubyPython.stop in the destructor, but unfortunately it seems that there is no destructor in Ruby:

class QDSHiveHelper
    def initialize
        RubyPython.start
        qds = RubyPython.import('blah')
        ...
    end
    def do_something
        qds.some_function
        ...
    end 
    def finalize
        RubyPython.stop
    end
end

Could someone please explain how to accomplish this? ObjectSpace.define_finalize seems to be discouraged and has some gotchas (can't use closure etc). I could also just leave RubyPython dangling and not call stop on it, but I don't know what could be the consequences. What's the best way out?

sawa
  • 165,429
  • 45
  • 277
  • 381
nonbeing
  • 6,907
  • 6
  • 36
  • 46
  • 1
    The "best way out" is to not call `RubyPython.start` in your constructor, where it will be invoked once per instance of your class. Instead, call it once per instance of your program. – user229044 Jun 17 '13 at 13:33
  • meagar: if you could post an answer with this, I'll accept it. This is the indeed what works best for me. – nonbeing Jun 20 '13 at 06:38
  • Your question has been marked as a duplicate, so no new answers can be posted. Don't worry about it :) – user229044 Jun 20 '13 at 13:52

1 Answers1

4

There is a hook called ObjectSpace.define_finalizer that is called when an object is destroyed.

sawa
  • 165,429
  • 45
  • 277
  • 381