As part of a larger project, I'm trying to "embed" an interactive Python interpreter in a Ruby process. I would like to do something like the following:
$ irb
irb(main):001:0> pipe = IO.popen("python", "w+")
=> #<IO:0x7f3dba4977e0>
irb(main):002:0> pipe.puts "print 'hello'"
=> nil
irb(main):003:0> pipe.gets
=> 'hello\n'
Unfortunately, getsit seems to be hanging, but not returning any way out of the Python process. I tried variations of this procedure using open3, using the r+instead mode w+, and a couple of other minor options ( python -uamong them) without success.
Is there a way to establish an interactive connection with the Python shell from Ruby - essentially “wrap” the Python CLI? I use Ruby 1.8.7 (2010-06-23 patchlevel 299) and Python 2.6.6 on an x86_64 machine, although I hope the solutions will be portable (ish) in Python versions.
source
share