Ryan Miller
Ryan Miller

Reputation: 391

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, 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.

Upvotes: 4

Views: 286

Answers (5)

Ryan Miller
Ryan Miller

Reputation: 391

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(',')

Upvotes: 0

knut
knut

Reputation: 27855

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

Upvotes: 3

Ryanmt
Ryanmt

Reputation: 3265

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')

Upvotes: 2

Holger Just
Holger Just

Reputation: 55758

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".

Upvotes: 8

Larsenal
Larsenal

Reputation: 51156

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"
          ...]

Upvotes: 3

Related Questions