lib/rubypython.rb in rubypython-0.2.3 vs lib/rubypython.rb in rubypython-0.2.4
- old
+ new
@@ -28,10 +28,37 @@
RubyPython.run do
cPickle=import "cPickle"
puts cPickle.dumps "RubyPython is still awesome!"
end
+The downside to the above method is that the block has no access to the encompassing scope. An
+alternative is to use <tt>RubyPython.session</tt>. The downside to this approach is that the module
+methods are not available by their unqualified names: i.e.
+ irb(main):001:0> RubyPython.session do
+ irb(main):002:1* cPickle=import "cPickle"
+ irb(main):003:1> end
+ NoMethodError: undefined method `import' for main:Object
+ from (irb):2
+ from ./rubypython.rb:93:in `call'
+ from ./rubypython.rb:93:in `session'
+ from (irb):1
+
+However:
+ irb(main):001:0> RubyPython.session do
+ irb(main):002:1* cPickle=RubyPython.import "cPickle"
+ irb(main):003:1> puts cPickle.dumps "RubyPython is still awesome!"
+ irb(main):004:1> end
+ S'RubyPython is still awesome!'
+ .
+ => nil
+
+A compromise can be achieved by just including the RubyPython module into the scope you're
+working in.
+
+If you really wish to be free of dealing with the interpreter, just import 'rubypython/session'.
+This will start the interpreter on import and will halt it when execution ends.
+
==Errors
The RubyPythonModule defines a new error object, PythonError. Should any error occur within
the Python interpreter, the class and value of the error will be passed back into ruby within
the text of the raised PythonError.
irb(main):001:0> RubyPython.start
@@ -62,23 +89,38 @@
# Used to end the python session. Adds some cleanup on top of RubyPythonBridge.stop
def self.stop() #=> true,false
ObjectSpace.each_object(RubyPythonBridge::RubyPyObject) do |o|
o.free_pobj
end
+ PyMain.main=nil
+ PyMain.builtin=nil
RubyPythonBridge.stop
end
# Import the python module +mod+ and return it wrapped as a ruby object
def self.import(mod)
RubyPythonBridge.import(mod)
end
# Handles the setup and cleanup involved with using the interpreter for you.
# Note that all Python object will be effectively scope to within the block
- # as the embedded interpreter will be halted at its end.
+ # as the embedded interpreter will be halted at its end. The supplied block is
+ # run within the scope of the RubyPython module.
+ #
+ # Alternatively the user may prefer RubyPython.session which simples handles
+ # initialization and cleanup of the interpreter.
def self.run(&block)
start
module_eval(&block)
stop
end
+
+ # Simply starts the interpreter, runs the supplied block, and stops the interpreter.
+ def self.session(&block)
+ start
+ retval=block.call
+ stop
+ return retval
+ end
+end
-end
\ No newline at end of file
+