How to initialize an array in one step using Ruby?

RubyArraysInitialization

Ruby Problem Overview


I initialize an array this way:

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

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

Ruby Solutions


Solution 1 - Ruby

You can use an array literal:

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

You can also use a range:

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

For arrays of whitespace-delimited strings, you can use Percent String syntax:

array = %w[ 1 2 3 ]

You can also pass a block to Array.new to determine what the value for each entry will be:

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

Finally, although it doesn't produce the same array of three strings as the other answers above, note also that you can use enumerators in Ruby 1.8.7+ to create arrays; for example:

array = 1.step(17,3).to_a
#=> [1, 4, 7, 10, 13, 16]

Solution 2 - Ruby

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

Solution 3 - Ruby

Along with the above answers , you can do this too

    =>  [*'1'.."5"]   #remember *
    => ["1", "2", "3", "4", "5"]




   

Solution 4 - Ruby

To prove There's More Than One Six Ways To Do It:

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

If 1.method(:+) wasn't possible, you could also do

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

Sure, it's overkill in this scenario, but if plus_1 was a really long expression, you might want to put it on a separate line from the array creation.

Solution 5 - Ruby

You can do

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

As others have noted, you can also initialize an array with %w notation like so:

array = %w(1 2 3)

or

array = %w[1 2 3]

Please note that in both cases each element is a string, rather than an integer. So if you want an array whose elements are integers, you should not wrap each element with apostrophes:

array_of_integers = [1, 2, 3]

Also, you don't need to put comma in 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 be three strings ---- "1,", "2,", "3". So if you do:

puts wrong_array

the output will be:

1,
2,
3
=>nil

which is not what we want here.

Hope this helps to clarify the point!

Solution 6 - Ruby

To create such an array you could do:

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

Solution 7 - Ruby

If you have an Array of strings, you can also initialize it like this:

array = %w{1 2 3}

just separate each element with any whitespace

Solution 8 - Ruby

You can initialize an array in one step by writing the elements in [] like this:

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

Solution 9 - Ruby

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

array = %w(1 2 3)

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser502052View Question on Stackoverflow
Solution 1 - RubyPhrogzView Answer on Stackoverflow
Solution 2 - RubypankajdohareyView Answer on Stackoverflow
Solution 3 - RubyRameshVelView Answer on Stackoverflow
Solution 4 - RubyAndrew GrimmView Answer on Stackoverflow
Solution 5 - Rubytg_soView Answer on Stackoverflow
Solution 6 - RubyReese MooreView Answer on Stackoverflow
Solution 7 - RubyJohn DouthatView Answer on Stackoverflow
Solution 8 - Rubysepp2kView Answer on Stackoverflow
Solution 9 - RubyPrabhakar UndurthiView Answer on Stackoverflow