113

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?

rochb
  • 2,249
  • 18
  • 26
user502052
  • 14,803
  • 30
  • 109
  • 188

9 Answers9

203

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]
Matilda Smeds
  • 1,384
  • 11
  • 18
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 4
    Plus one for the detailed answer, even though I prefer splat over `to_a` (`[*'1'..'3']`). – Michael Kohl Feb 05 '11 at 17:48
  • @MichaelKohl I agree; I was under the (mistaken) impression that you couldn't splat ranges in 1.8.6. I'll add that, thanks! Note that you don't need to splat within an array literal (unless you're compositing along with the splat). – Phrogz Feb 05 '11 at 17:50
  • I know, it's just that I mostly use splat for that purpose (compositing that is) and I also like that it shows off what you end up with. – Michael Kohl Feb 05 '11 at 19:39
  • 2
    Also, the class method Array::[] can be used: `Array[ "1","2","3"] #=> ["1","2","3"]` (I don't think this method has anything to do with the array literal constructor). You can also use the top-level Kernel#Array (method name does look like a class name) `Array(1..5) #=> [1,2,3,4,5]` – b1_ Jun 07 '12 at 11:19
  • what the * does? where I can find documentation for that? – Arnold Roa Dec 30 '15 at 00:20
  • @ArnoldRoa See the sections on "Array Coercion" [on this link](https://endofline.wordpress.com/2011/01/21/the-strange-ruby-splat/) or [on this link](http://jacopretorius.net/2012/01/splat-operator-in-ruby.html). – Phrogz Dec 30 '15 at 03:45
26

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']
pankajdoharey
  • 1,562
  • 19
  • 30
  • 8
    I didn't down vote it, but I'm guessing because this invoked three methods and incrementally grows the array, as opposed to `[1,2,3]` which does a single initialization. Also, yours is more characters. Also, you have created an array of Fixnums whereas the OP was asking about an array of strings. – Phrogz Dec 09 '11 at 21:57
8

Along with the above answers , you can do this too

    =>  [*'1'.."5"]   #remember *
    => ["1", "2", "3", "4", "5"]
RameshVel
  • 64,778
  • 30
  • 169
  • 213
7

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.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
3

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!

tg_so
  • 496
  • 6
  • 11
2

To create such an array you could do:

array = ['1', '2', '3']
Reese Moore
  • 11,524
  • 3
  • 24
  • 32
2

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

John Douthat
  • 40,711
  • 10
  • 69
  • 66
1

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

array = ['1', '2', '3']
sepp2k
  • 363,768
  • 54
  • 674
  • 675
0

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

Prabhakar
  • 6,458
  • 2
  • 40
  • 51