Let’s say for whatever reason, I am going to define an array variable in a ruby script file that holds all the US states as strings. What is a clean way of doing this? And by clean, I’m not really looking for performance, but more readability.
Here are a couple ways I have tried, but don’t really like:
A single LONG line definition. I don’t care for this because it is very long, or needs to be word wrapped.
states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", ...]
Another options would be a multiple line definition, pushing strings to the array. This resolves the long lines (width-wise), but I don’t care for the mixed use of assignment and array.push.
states = ["Alabama", "Alaska", "Arizona"]
states.push("Arkansas", "California", "Colorado")
states.push("...")
Yet another option would be single line pushes. This seems consistent, but could be quite long to accomplish.
states = []
states.push("Alabama")
states.push("Alaska")
states.push("Arizona")
states.push("...")
Now, sure, ideally, I would not be hard-coding my array values and should be pulling them from a database or web service, etc. But, for the purpose of the question, let’s assume that the values do not exist anywhere else currently.
Just put them on multiple lines if you like…
The key is to end the lines with commas. The following won’t work: