Ruby - Array Arrays Creating


Ruby uses square brackets to delimit an array.

You can create an array, fill it with some comma-delimited values, and assign it to a variable:

arr = ['one','two','three','four'] 

Ruby arrays are objects. They are defined by the Array class and indexed from 0.

You can reference an item in an array by placing its index between square brackets.

If the index is invalid, nil is returned:


arr = ['a', 'b', 'c'] 
puts(arr[0])      # shows 'a' 
puts(arr[1])      # shows 'b' 
puts(arr[2])      # shows 'c' 
puts(arr[3])      # nil
# from w  w  w  .j  a  v a 2  s .c o  m


Related Topic