Storing hashes in a file
16 Jun 2014If you ever need to store data in a file and then retrieve it, consider using PStore, a standard Ruby library specifically for this purpose. I found the documentation not terribly clear, but basically here’s how it works:
We first need a PStore object to work with.
This just creates the object, we haven’t created a file or written to it yet. let’s create some hashes:
This last line is very important, without it the system will not write anything to file. the docs say you can alternatively just use end without the commit and the file will be written, but that did not work for me.
To read the stored hashes
This is an easy way to store data in hashes, and who doesn’t love hashes?
Use YAML instead of Marshal to store those hashes
But wait, there’s more! Once you get the hang of this hash-storing trick, switch to using YAML::Store
instead of PStore
. The reason is simple: yaml
files are much more readable than PStore
files which are not readable at all by humans.
Yaml::Store uses yaml to write the data whereas PStore uses Marshal.
Learn more about YAML::Store here, but basically you can keep everything pretty much the same just: