Python中文
首页
教程
问答
标签
搜索
登录
注册
python中ruby函数的返回值
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个由python脚本执行的ruby脚本。在python脚本中,我想访问ruby函数的返回值。在</p> <p>想象一下,我会有一个<strong>ruby脚本</strong><em>测试.rb</em>:</p> <pre><code>class TestClass def self.test_function(some_var) if case1 puts "This may take some time" # something is done here with some_var puts "Finished" else # just do something short with some_var end return some_var end end </code></pre> <p>现在,我想把函数的返回值放入python脚本中,打印的输出应该是stdout。在</p> <p>我尝试了以下方法(<strong>示例1</strong>):</p> ^{pr2}$ <p>但是,这给出了stdout的全部输出,<code>answer</code>只是退出代码。在</p> <p>因此,我尝试了这个(<strong>示例2</strong>):</p> <pre><code>from subprocess import check_output answer = check_output(["ruby", "-r", "test.rb", "-e", "puts TestClass.test_function('some meaningful text')"]) </code></pre> <p>这给了我函数在<em>else情况下的返回值(参见测试.rb)几乎马上。但是,如果<em>case1</em>为真,<code>answer</code>包含整个输出,但是在运行时测试.rb什么都不会印出来。在</p> <p>有什么方法可以得到ruby函数的返回值和打印到stdout的语句吗?理想情况下,该解决方案不需要安装其他模块。此外,我不能更改ruby代码。在</p> <p>编辑:</p> <p>也尝试过这样做,但是在运行ruby脚本时,这也没有给出stdout的输出(<strong>示例3</strong>):</p> <pre><code>import subprocess process = subprocess.Popen(["ruby", "-r", "test.rb", "-e", "puts TestClass.test_function('some meaningful text')"], stdout=subprocess.PIPE) answer = process.communicate() </code></pre> <p>我还认为这与在ruby脚本中将输出刷新到stdout无关。示例1立即给出了输出。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>查看<a href="http://docs.python.org/library/subprocess.html#popen-constructor" rel="nofollow">http://docs.python.org/library/subprocess.html#popen-constructor</a>并查看刷新stdout的ruby方法。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何用if条件捕获函数返回值
7 回答
如何用if语句判断列表中是否存在该索引?
1 回答
如何用if语句向量化numpy数组中的最大值?
2 回答
如何用IF语句有条件地保存零碎的结果?
1 回答
如何用if语句测试异常对象?
3 回答
如何用IF语句编写二元函数
9 回答
如何用igraph在python中创建顶点权重的图?
1 回答
如何用ijson和python解析json
6 回答
如何用iloc求子矩阵
10 回答
如何用Imagemagick或PIL绘制高质量的图像笔划(边框)?
3 回答
如何用importlib在python中动态导入模块?
5 回答
如何用import语句重写python内置函数?
1 回答
如何用imshow混合裁剪的强度并显示正确的混合强度?
7 回答
如何用in dictionary解析havin dictionary中的json文件
3 回答
如何用in-Django URL替换%20
10 回答
如何用in\op正确构造查询
4 回答
如何用inbuild对象替换文件
5 回答
如何用inheritan类实现flask restful
1 回答
如何用intersphinx正确地编写对外部文档的交叉引用?
3 回答
如何用int修改LpVariable?
1 回答