Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Ruby Programming
Previous Page Home Next Page

Arrays

The class Array holds a collection of object references. Each object reference occupies a position in the array, identified by a non-negative integer index.

You can create arrays using literals or by explicitly creating an Array object. A literal array is simply a list of objects between square brackets.

a = [ 3.14159, "pie", 99 ]
a.type Array
a.length 3
a[0] 3.14159
a[1] "pie"
a[2] 99
a[3] nil
b = Array.new
b.type Array
b.length 0
b[0] = "second"
b[1] = "array"
b ["second", "array"]

Arrays are indexed using the [] operator. As with most Ruby operators, this is actually a method (in class Array) and hence can be overridden in subclasses. As the example shows, array indices start at zero. Index an array with a single integer, and it returns the object at that position or returns nil if nothing's there. Index an array with a negative integer, and it counts from the end. This is shown in Figure 4.1 on page 35.

Figure not available...

a = [ 1, 3, 5, 7, 9 ]
a[-1] 9
a[-2] 7
a[-99] nil

You can also index arrays with a pair of numbers, [start, count]. This returns a new array consisting of references to count objects starting at position start.

a = [ 1, 3, 5, 7, 9 ]
a[1, 3] [3, 5, 7]
a[3, 1] [7]
a[-3, 2] [5, 7]

Finally, you can index arrays using ranges, in which start and end positions are separated by two or three periods. The two-period form includes the end position, while the three-period form does not.

a = [ 1, 3, 5, 7, 9 ]
a[1..3] [3, 5, 7]
a[1...3] [3, 5]
a[3..3] [7]
a[-3..-1] [5, 7, 9]

The [] operator has a corresponding []= operator, which lets you set elements in the array. If used with a single integer index, the element at that position is replaced by whatever is on the right-hand side of the assignment. Any gaps that result will be filled with nil.

a = [ 1, 3, 5, 7, 9 ] [1, 3, 5, 7, 9]
a[1] = 'bat' [1, "bat", 5, 7, 9]
a[-3] = 'cat' [1, "bat", "cat", 7, 9]
a[3] = [ 9, 8 ] [1, "bat", "cat", [9, 8], 9]
a[6] = 99 [1, "bat", "cat", [9, 8], 9, nil, 99]

If the index to []= is two numbers (a start and a length) or a range, then those elements in the original array are replaced by whatever is on the right-hand side of the assignment. If the length is zero, the right-hand side is inserted into the array before the start position; no elements are removed. If the right-hand side is itself an array, its elements are used in the replacement. The array size is automatically adjusted if the index selects a different number of elements than are available on the right-hand side of the assignment.

a = [ 1, 3, 5, 7, 9 ] [1, 3, 5, 7, 9]
a[2, 2] = 'cat' [1, 3, "cat", 9]
a[2, 0] = 'dog' [1, 3, "dog", "cat", 9]
a[1, 1] = [ 9, 8, 7 ] [1, 9, 8, 7, "dog", "cat", 9]
a[0..3] = [] ["dog", "cat", 9]
a[5] = 99 ["dog", "cat", 9, nil, nil, 99]

Arrays have a large number of other useful methods. Using these, you can treat arrays as stacks, sets, queues, dequeues, and fifos. A complete list of array methods starts on page 278.
Ruby Programming
Previous Page Home Next Page

 
 
  Published under the terms of the Open Publication License Design by Interspire