Arrays in ruby code example

Example 1: ruby create array

array = Array.new

Example 2: how to make a new array ruby

my_array = []
# OR
my_array = [1, 2, 3]
# OR
my_array = Array.new
#OR
(1..10).to_a

Example 3: ruby array

ary = [1, "two", 3.0] #=> [1, "two", 3.0]

Example 4: Ruby Array.new syntax

Array.new( number of indices, value of the indices ) { optional block for value of indices }

ary = Array.new    #=> []
Array.new(3)       #=> [nil, nil, nil]
Array.new(3, true) #=> [true, true, true]
Array.new(3) { Hash.new } #=> [ {}, {}, {} ]

Tags:

Ruby Example