如何捕获 pry shell 命令的输出?
我正在使用 pry,我想捕获并处理 shell 命令的输出。
例如,如果我运行,
pry(main)> .ls
我想将文件列表放入一个可以在 Ruby 中使用的数组中。
我该怎么做?
I'm using pry and I want to capture, and work with output of a shell command.
For example, If I run
pry(main)> .ls
I want to get the list of files into an array that I can work with in Ruby.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个很老的问题,但无论如何我都会回答它。从 pry 命令获取数据的主要方法有两种。第一个是该命令是否将 keep_retval 选项设置为 true,而 shell 命令则不会。第二种,是使用虚拟管道。在您的示例中,可以这样做:
This is a pretty old question but I'll answer it anyways. There are two primary methods of getting data out of pry commands. The first is if the command sets the
keep_retval
option to true, which the shell command does not. The second, is to use the virtual pipe. In your example this can be done as:我认为这是某种 pry 的魔力;-)
快速查看发生的情况后(我没有查看 pry 的源代码),您可能想使用这个:
或者
这个解决方案的好处是它可以在
之外工作>撬动
I assume it's some kind of pry's magic ;-)
After quick look at what's happening (I didn't look at pry's source), you might want to use this:
or
What's good about this solution is that it will work outside of
pry