I am dealing with a csv file that has some customer information (email, name, address, amount, [shopping_list: item 1, item 2]).
I would like work with the data and produce some labels for printing… as well as to gather some extra information (total amounts, total items 1…)
My main concern is to find the appropriate structure to store the data in ruby for future manipulation. For now I have thought about the following possibilities:
- multidimensional arrays: pretty simple to build, but pretty hard to access the data in a beautiful ruby way.
- hashes: having the email as key, and storing the information in different hashes (one hash for name, another hash for address, another hash for shopping list…)
- (getting the cvs data in to a Database and working with the data from ruby??)
I would really appreciate your advice and guidance!!
Once you have more than a couple pieces of information that you need to group together, it’s time to consider moving from a generic hash/array to something more specialized. A good candidate for what you’ve described is Ruby’s
structmodule:Output:
Struct#newsimply creates a class with anattr_accessorfor each symbol you pass in. Well, it actually creates a bit more than that, but for starters, that’s all you need to worry about.Once you’ve got the data from each row packed into an object of some sort (whether it’s a
structor a class of your own), then you can worry about how to store those objects.