0

I'm new to ruby and have written a program that takes several lines of data (actually a JSON) and converts it into a table in the command line. Everything works fine with the JSON data embedded in the program but I would like to have it prompt the user to paste the data into the command line. I know about gets and chomp, but since a JSON is formatted with multiple lines/carriage returns, when I paste in the command line it takes each line as a separate entry. I feel like the answer is simple but I'm having a hard time finding info online. I just want it to take everything I paste all at once and ignore all carriage returns.

Any suggestions?

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Erk
  • 69
  • 1
  • 1
  • 7
  • Here is a similiar question, with a not so pretty answer: http://stackoverflow.com/questions/13839940/ruby-gets-that-works-over-multiple-lines - Otherwise: Would it be an option to prompt the user for a file name / path, and then read the json from the file? – trueunlessfalse Aug 09 '16 at 18:58

1 Answers1

0

If you're taking input via the console, $stdin in technical parlance, and you want to receive multiple, independent multi-line objects you'll need to have some kind of delimiter.

This could be as simple as one or more blank lines between each JSON object, or it could be a marker like END or --.

It depends on how your JSON data is formatted, as blank lines within a JSON object are valid, yet are not normally emitted by most JSON generators.

Don't forget that the UNIX model strongly encourages you to be able to do things like this:

processor < input.json

Or things like this:

processor *.json

Where you can receive multiple files via ARGV and process those sequentially. That avoids a lot of this mess.

tadman
  • 208,517
  • 23
  • 234
  • 262