Ruby - Array Arrays Creating

Introduction

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:

Demo

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  ww  . j a va  2  s  . com

Result

Related Topic