How to initialize an array in one step using Ruby?

后端 未结 9 753
情话喂你
情话喂你 2021-01-30 00:08

I initialize an array this way:

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

Is it possible to do that in one s

9条回答
  •  感情败类
    2021-01-30 00:34

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

提交回复
热议问题