I am using an array which contains the results of a database-query, which is later formatted as html (for a webapplication) or as csv (for import in a spreadsheet). I want to attach information to the array-element which has some additional information about how the data of this element can be used.
For instance, array-element-data...
- ... can be displayed as a link: then I want the link information attached. The code which creates html from the array can use it to create a link.
- ... is a date of the form
2009-09-14
: then I want to somehow flag it as being a date. If the usage is for a html-page, it can then be displayed somewhat more beautiful, e.g. Mo Sep 14 or Today; if the recipient is a csv it would be best to leave it.
Is there a best-practice way of solving this problem?
I did think of several possible solutions, but wanted to ask if someone already knows a "best practice". From possibly best to worst:
- Store each array-element as custom-created object (Date,Linkable,Text...), instead of having the array-element as text. Possibly provide a default
.to_string()
method. - Make each array-element a hash, so that I could say
a[5][7]['text']
ora[5][7]['link']
. - Make different versions of the array, e.g.
textArray[5][7]
,linkArray[5][7]
Creating the html as a start and just using the text-version seems like a bad idea, as the appearance differs depending on the usage (e.g. 2009-09-14 vs Mo Sep 14).
Or am I just asking the wrong question?