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

Say for any reason, I'm going to define an array variable in a ruby ​​script file that contains all US states as strings. What is a clean way to do this? And I’m not very clean looking for work, but I read more.

Here are some ways I've tried but don't really like it:

Single line definition of LONG . I don’t care about it, because it is very long, or it needs to be wrapped in words.

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

Other parameters may be the definition of several lines, pushing lines into an array. This allows long strings (in width), but I don't care about the mixed use of assignment and array.push.

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

Another option is single-line clicks. It seems consistent, but can be quite long to achieve.

 states = [] states.push("Alabama") states.push("Alaska") states.push("Arizona") states.push("...") 

Now, of course, ideally, I would not hardcode the values ​​of my array and should retrieve them from a database or web service, etc. But for the purpose of the question, let me suppose that meanings do not exist elsewhere.

+4
source share
5 answers

Just put them on several lines if you want ...

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

The key must end with commas. The following actions will not be performed:

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

In Ruby, there is an actual syntax element for defining such arrays:

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

Note that it will divide the elements into spaces. Thus, a record similar to North Dakota will consist of two elements: North and Dakota.

+8
source

You can create your data using the line:

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

Advantage: You can 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 
+3
source

Use this trick, you will find it to solve the problem quite accurately.

 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 can also suggest storing data on separate lines in a file, which can then be generated as an array with a simple command:

 array = File.readlines('input.txt') 
+2
source

Assuming that the states (array values) MUST be defined in the script file itself, based on all the other answers provided, I would probably save the states in a separate file, separated by commas, and do it

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

Source: https://habr.com/ru/post/1369555/


All Articles