Array with Array.new : Array.new « Array « Ruby






Array with Array.new


empty = Array.new       # []: returns a new empty array
nils = Array.new(3)     # [nil, nil, nil]: new array with 3 nil elements
zeros = Array.new(4, 0) # [0, 0, 0, 0]: new array with 4 0 elements
copy = Array.new(nils)  # Make a new copy of an existing array
count = Array.new(3) {|i| i+1}  # [1,2,3]: 3 elements computed from index

 








Related examples in the same category

1.Array.new creates an empty array
2.Array.new(2) creates an array with two nil values
3.Array.new(5, "A") fills an array with five a
4.Create an array of hashes
5.Fill an array of hashes
6.Array initialized by copying