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: 2021-05-16

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

  {:data=>{"id"=>2690, "url"=>"", "filename"=>"", "content_type"=>nil},
  :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),

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.


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