11

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Elijah Murray
  • 2,132
  • 5
  • 30
  • 43
  • Why through PRY? That's not what it's used for. Instead of deciding it has to be through PRY, why don't you explain more about the code you're using, and what type of data (logging, variable dumps, JSON/YAML data). As is you haven't told us much of use and we need to know a lot more otherwise this question will likely suffer a short life. – the Tin Man Jan 07 '14 at 18:48
  • Ok. 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. – Elijah Murray Jan 07 '14 at 18:54
  • Please put that information in your question, not a comment. Don't expect potential answerers to read every comment to figure out what you're doing. You have to help us help you. – the Tin Man Jan 07 '14 at 18:56

2 Answers2

21

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 1
    If you `File.write('test.data', JSON.pretty_generate(hash))` your day will be prettier. – bonafernando Feb 28 '20 at 19:46
  • 1
    It will also consume a lot more memory, and be slower to transmit; Viewing it when debugging is one thing, but don't do that in production. – the Tin Man Feb 29 '20 at 04:39
3

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.

Motine
  • 1,638
  • 18
  • 18