Never copy raw ruby console output when intending to use the data again

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the ruby category.

Last Updated: 2024-11-21

During a data migration on a server, I generated data about problematic entries. The output on screen was something like:

[
  {:data=>{"id"=>2690, "url"=>"https://s3.amazonaws.com/oxbridgenotes/zips/2690/original/Chemistry_of_Biological_Molecules.zip", "filename"=>"Chemistry_of_Biological_Molecules.zip", "content_type"=>nil},
  :s3_url=>
    "https://oxbridgenotes.s3.amazonaws.com/zips/2690/original/Chemistry_of_Biological_Molecules.zip?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=asdfasfdasfdasfasfasdfF20190919%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20190919T162631Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=777777777711770836a40c7675fa64f95fc3f7deed6e562d3bd6456bdc29a025",
  :klass=>ZipFile(id: integer, zip_file_name: string, created_at: datetime,
  updated_at: datetime, released_on: date, zippable_id: integer, zippable_type:
  string, zip_file_size: integer),
  :record_id=>2690},
  ...
]

I copied this output in the console, intended to use it later. This seemed reasonable because it looked like "normal ruby".

But I was wrong. It looked like normal ruby output but was certainly not normal Ruby code that could be input. It took me half an hour to massage the records back into something usable that could be pasted into a console. For example, the :klass bit with ZipFile was unusable. The => did not have enough whitespace on either side, etc.

In future, I should have called .to_json before copying out the data into the past buffer => this is a generally reusable format that's much more portable.

Lesson

Do not copy console output and expect it to be re-usable in a program. Instead explicitly convert.