开发者

What is a clean way of defining an array of strings in Ruby?

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,开发者_StackOverflow 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.


There's an actual syntax element in ruby for defining such arrays:

> states = %w(Alabama Alaska Arizona
>    Arkansas California
>    Colorado)
=> ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado"]

Note though, that it will split the elements on whitespace. So an entry like "North Dakota" will end up as two items: "North" and "Dakota".


Just put them on multiple lines if you like...

states = ["Alabama",
          "Alaska",
          "Arizona",
          "Arkansas",
          "California",
          "Colorado",
          ...]

The key is to end the lines with commas. The following won't work:

# Does not work!
states = ["Alabama"
          ,"Alaska"
          ,"Arizona"
          ,"Arkansas"
          ,"California"
          ,"Colorado"
          ...]


You could build your data via a string:

states = %{Alabama
  Alaska
  Arizona
  Arkansas
  California
  Colorado
  Nort Carolina
}.each_line.map{|s| s.strip}

p states

Advantage: You could store your data in a Text file.

states = states = File.readlines('countries.txt').map{|s| s.strip}

And countries.txt:

Alabama
Alaska
Arizona
Arkansas
California
Colorado
Nort Carolina


Use this trick, you'll find it to solve the problem rather neatly.

ruby-1.9.2-p290 :001 > text =  %w( hello yo wazzap
ruby-1.9.2-p290 :002]> hi
ruby-1.9.2-p290 :003]> hello)
 => ["hello", "yo", "wazzap", "hi", "hello"] 
ruby-1.9.2-p290 :004 > text
 => ["hello", "yo", "wazzap", "hi", "hello"] 

I might also suggests the storing of the data on individual lines in a file, which can then be generated as an array with the simple command:

array = File.readlines('input.txt')


Assuming that the states (array values) do not NEED to be defined in the script file itself, based on the all other answers provided, I would probably store the states in a separate comma-separated file and do this

states = File.read('states.txt').split(',')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜