How to initialize an array in one step using Ruby?

I initialize the array this way:

array = Array.new array << '1' << '2' << '3' 

Is it possible to do this in one step? If so, how?

+79
arrays initialization ruby
Feb 05 '11 at 17:36
source share
9 answers

As others noted, you can use an array literal:

 array = [ '1', '2', '3' ] 

You can also use the range for your example:

 array = ('1'..'3').to_a # parentheses are required # or array = *('1'..'3') # parentheses not required, but included for clarity 

For arrays of many lines with delimiters simple in this notation:

 array = %w[ 1 2 3 ] 

In general, you can pass a block to Array.new and use it to determine what value for each record will be:

 array = Array.new(3){ |i| (i+1).to_s } 

Finally, although it does not create the same three-string array as the other answers above, note also that you can use enums in Ruby 1.8.7+ to create arrays; eg:

 array = 1.step(17,3).to_a #=> [1, 4, 7, 10, 13, 16] 
+153
Feb 05 '11 at 17:43
source share
β€” -

Oneliner:

 array = [] << 1 << 2 << 3 #this is for fixnums. 

or

  a = %w| 1 2 3 4 5 | 

or

  a = [*'1'..'3'] 

or

  a = Array.new(3, '1') 

or

  a = Array[*'1'..'3'] 
+11
Sep 28 '11 at 8:14
source share

Along with the answers above, you can also do this

  => [*'1'.."5"] #remember * => ["1", "2", "3", "4", "5"] 
+8
Apr 21 '11 at 20:26
source share

To prove there are more than one six ways to do this:

 plus_1 = 1.method(:+) Array.new(3, &plus_1) # => [1, 2, 3] 

If 1. method (: +) is not possible, you can also do

 plus_1 = Proc.new {|n| n + 1} Array.new(3, &plus_1) # => [1, 2, 3] 

Of course, in this scenario it overflows, but if plus_1 was a really long expression, you can put it on a separate line from the array creation.

+7
Sep 29 '11 at 23:55
source share

If you have an array of strings, you can also initialize it as follows:

array = %w{1 2 3}

just separate each element with any space

+3
Feb 05 2018-11-17T00:
source share

To create such an array, you can do:

 array = ['1', '2', '3'] 
+2
Feb 05 '11 at 17:39
source share

You can do

 array = ['1', '2', '3'] 

As others noted, you can also initialize the array with the designation% w as follows:

 array = %w(1 2 3) 

or

 array = %w[1 2 3] 

Note that in both cases, each element is a string, not an integer. Therefore, if you want an array whose elements are integers, you should not wrap each element with apostrophes:

 array_of_integers = [1, 2, 3] 

In addition, you do not need to enter a comma between the elements (which is necessary when creating an array without this% w notation). If you do this (which I often did by mistake), as in:

 wrong_array = %w(1, 2, 3) 

its elements will consist of three lines: "1", "2", "3". So if you do this:

 puts wrong_array 

the output will be:

 1, 2, 3 =>nil 

what we do not want here.

Hope this helps clarify the point!

+2
Mar 08 '16 at 21:51
source share

You can initialize the array in one step by writing the elements in [] as follows:

 array = ['1', '2', '3'] 
+1
Feb 05 '11 at 17:40
source share

You can simply do this with the %w notation in ruby ​​arrays.

 array = %w(1 2 3) 

It will add array values ​​1,2,3 to the array and print the output as ["1", "2", "3"]

0
Mar 08 '16 at 16:08
source share