Created
April 6, 2016 00:37
-
-
Save gr33n7007h/a84bc368634e11fefdbe903863bed9e7 to your computer and use it in GitHub Desktop.
Run Python code from Ruby with Fiddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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