Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Is this possible with Pry? I looked at the documentation but doesn't seem like there's an easy way to dump console data anywhere.

like image 665
Elijah Murray Avatar asked Jan 07 '14 18:01

Elijah Murray


2 Answers

I have a hash, with nested hashes/objects, which I need to send over to a 3rd party for work with an 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.

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. 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. 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.

like image 112
the Tin Man Avatar answered Oct 19 '22 18:10

the Tin Man


You can also export any string into a file (here 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.

like image 41
Motine Avatar answered Oct 19 '22 19:10

Motine