Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Created April 6, 2016 00:37
Show Gist options
  • Save gr33n7007h/a84bc368634e11fefdbe903863bed9e7 to your computer and use it in GitHub Desktop.
Save gr33n7007h/a84bc368634e11fefdbe903863bed9e7 to your computer and use it in GitHub Desktop.
Run Python code from Ruby with Fiddle
#!/usr/bin/env ruby
require 'fiddle'
require 'fiddle/import'
LIBPY = '/usr/lib64/libpython3.so'
module Py
extend Fiddle::Importer
dlload LIBPY
extern 'void Py_Initialize( void )'
extern 'int PyRun_SimpleString( const char * )'
end
Py.Py_Initialize
Py.PyRun_SimpleString("print([x*x for x in range(10)])") #=> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Py.PyRun_SimpleString("*s,='abcdef'; print(s)") #=> ['a', 'b', 'c', 'd', 'e', 'f']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment