# we'll make an array of fruit fruit = ['oranges', 'apples', 'pears', 'tomatoes']
Now lets display the array:
Now lets play with the array:
#display first element in array fruit #display third element in array fruit #undefined element will display nil fruit(this is an example of freedom ruby allows compared to other languages like java or c++) #display array first from the right fruit[-1] #display array second from the right fruit[-2] #display first three elements of array fruit[0..2] #... add some more ...
Here is an array of an array, ie a multidimensional array:
groups = [[fruit], ['onion', 'potato', 'mushroom', 'celery'], ['milk', 'cheese', 'yogurt', 'otherdairy']] # the first array a #likewise this will show the 3rd element of the second # element array a #...you play around some more ...
This is the end of my example by show tutorial of arrays. Hope you enjoyed. My next post will be after the holidays.
Oh one more thing about arrays, but not the last:
Arrays can also be used as a linked list, a stack, or a set.
Edited by Roger, 06 January 2011 - 12:01 PM.