rubypython
Get Version
0.5.3contents
description
RubyPython is a bridge between the Ruby and Python interpreters. It embeds a running Python interpreter in the Ruby application’s process using FFI and provides a means for wrapping, converting, and calling Python objects and methods.
RubyPython uses FFI to marshal the data between the Ruby and Python VMs and make Python calls. You can:
Inherit from Python classes.
Configure callbacks from Python.
Run Python generators (on Ruby 1.9.2 or later).
where
The RubyPython homepage, project description, and main downloads can be found onRubyForge.
Source is kept in sync between Bitbucket and GitHub, but the Bitbucket repository is the canonical repository and where the issue tracker resides. We use Hg-Git to keep the two repositories in sync.
synopsis
RubyPython is fairly easy to start using; there are three phases to its use:
Start the Python interpreter (RubyPython.start).
Import and use Python code (RubyPython.import).
Stop the Python interpreter (RubyPython.stop).
There are also two methods, RubyPython.session and RubyPython.run that will start before running the code provided in the block and stop it afterwards.
basic usage
require "rubypython"
RubyPython.start # start the Python interpreter
cPickle = RubyPython.import("cPickle")
p cPickle.dumps("Testing RubyPython.").rubify
RubyPython.stop # stop the Python interpreterspecific python version
require "rubypython"
RubyPython.start(:python_exe => "python2.7") # Can also be a full path
cPickle = RubyPython.import("cPickle")
p cPickle.dumps("Testing RubyPython.").rubify
RubyPython.stop # stop the Python interpretervirtualenv
# Easy
RubyPython.start_from_virtualenv("/path/to/virtualenv")
# Or verbose
RubyPython.start(:python_exe => "/path/to/virtualenv/bin/python")
RubyPython.activateiterator support
# Python
def readfile():
for line in open("/some/file"):
yield line
# Ruby
readfile.to_enum.each do |line|
puts line
end
# Python
def iterate_list():
for item in [ 1, 2, 3 ]:
yield item
# Ruby
items = []
iterate_list.to_enum.each { |item| items << item }
puts items == [ 1, 2, 3 ] # => truepython to ruby callbacks
# Python
def simple_callback(callback, value):
return callback(value)
# Ruby
simple_callback(lambda { |v| v * v }, 4) # => 16
def triple(v)
v * 3
end
simple_callback(method(:triple), 4) # => 12
浙公网安备 33010602011771号