Thursday, February 28, 2008

Executing python within Ruby

If you have a need to execute Python within Ruby this might be interesting to you.

Executing python commands:



#!/usr/bin/env ruby
require 'rpy'
Py.start # starts the python interperter

output = "hello world"

Py.run %Q(print "#{output}")

Py.stop # stops the interperter


Retrieving a result set from python:



require 'yaml'
require 'rpy'
Py.start

result = YAML.load( Py.run( %Q(_rpython_result = {'hello':1,'world':2}), :serialize => 'yaml') )

puts result['hello']
puts result['world']

Py.stop

0 comments:

Reading list