O'Reilly logo

Ruby Quick Syntax Reference by Matt Clements

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 4

image

Arrays

Ruby arrays are integer–indexed arrays starting at position 0, rather like Java or C. Negative values can be used to retrieve values from the array from the end, so −1 would show the last element of an array, and −2 would show the second from last element.

2.1.0 :001 > array = [1,10,3,1] => [1, 10, 3, 1]2.1.0 :002 > array[0] => 12.1.0 :003 > array[-1] => 12.1.0 :004 > array[-2] => 3

Creating Arrays

An array can be created within ruby in a number of different syntaxes, the simplest is the use of the literal constructor []. Arrays are not type dependent, and therefore can contain multiple data types such as another array, a string ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required