简体   繁体   English

如何将Pry输出转储到文件或Vim?

[英]How do I dump Pry output to a file or Vim?

I have a Rails application, and I'm trying to export data, but directly through Pry because I only need to do it once. 我有一个Rails应用程序,我正在尝试导出数据,但直接通过Pry,因为我只需要执行一次。

Is this possible with Pry? Pry有可能吗? I looked at the documentation but doesn't seem like there's an easy way to dump console data anywhere. 我查看了文档,但似乎没有一种简单的方法可以在任何地方转储控制台数据。

I have a hash, with nested hashes/objects, which I need to send over to a 3rd party for work with an API. 我有一个带有嵌套哈希/对象的哈希,我需要将它发送给第三方以便使用API​​。 They need a dump of the data so they can set up the receiving end of my call. 他们需要转储数据,以便他们可以设置我的通话接收端。 I'm just going to do this in Ruby now, but it would have made more sense to dump the data through PRY, rather than edit my ruby object to dump the data, which I only need once. 我现在要在Ruby中执行此操作,但通过PRY转储数据更有意义,而不是编辑我的ruby对象来转储数据,我只需要一次。

If you can start the server from a local command-line, or SSH to the host and run an instance there, you can use Pry for this. 如果您可以从本地命令行启动服务器,或者通过SSH连接到主机并在那里运行实例,则可以使用Pry进行此操作。 Basically you need to add these lines to your code at the appropriate place: 基本上,您需要在适当的位置将这些行添加到代码中:

require 'pry-debugger'; binding.pry

which will stop your code and put you at the Pry prompt. 这将停止你的代码并让你在Pry提示符。 At that point you can enter something like: 此时您可以输入以下内容:

require 'json'
File.write('test.data', hash.to_json)

Read the Pry.debugger documentation for information about using Pry with remote Rails sessions, which might work better for you. 阅读Pry.debugger文档,了解有关使用Pry与远程Rails会话的信息,这可能对您更有效。

You can also export any string into a file (here output.txt ): 您还可以将任何字符串导出到文件中(此处为output.txt ):

x = 'something funky'
.echo '#{x}' > output.txt

Just be careful with quotes in the string. 请小心字符串中的引号。 These may lead to problems in the shell. 这些可能会导致shell出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM