[LRUG] Best way to serialise a Ruby object?

Olly Legg ollylegg at gmail.com
Thu Jul 14 02:31:01 PDT 2011


> What's the best way these days to serialise a Ruby object for storage in a SQL database field, and de-serialise it back again?


Rails uses YAML, so its probably not a bad bet.

> Marshal.dump would be the obvious choice.  Just make sure you're
> either storing it as binary or converting to base64 first, to avoid
> interesting encoding/decoding problems in mysql.

Rails used to use Marsahl.dump by default but, as far as I understand it, it's not guaranteed to be readable by future ruby versions. So when people upgraded Ruby they could no longer de-serialize the data they had already stored.

This prompted switching the default to YAML. However this looks like it wasn't as foolproof as they intended.


More information about the Chat mailing list