Ruby – Arrays & Hashes

Ruby has a built-in class of Arrays and Hashes. These two classes have large interfaces and will take time to master. Ruby has also a block syntax that lets you encapsulate chunks of code. These blocks can become powerful iterator constructs.

Arrays

The class Arrays holds a collection of object references. Each object reference occupies a position in the array identified by a non-negative integer index. Arrays are indexed using the [] operator which is an instance method of class Array and hence can be overridden in subclasses.

a = [1 , 2 , 3]

a[0] #=> 1

a[1] #=> 2

a[2] #=> 3

You can also index arrays with a pair of numbers, [start, count].

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

a[1, 3] #=> [3, 5, 7]

a[3, 1] #=> [7]

a[-3, 2] #=> [5, 7]

You can also index arrays using ranges. [..] includes the end position & […] 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]

[]= operator lets you set elements in the array.

a = [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]

a[2, 2]

If []= contains two numbers (a start and a length) or a range, then the elements in the original array are replaced to whatever is on the right size of the assignment.

a = [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..6] = [99, 98] #=> [1, 9, 8, nil, nil, 99, 98]

The Array class has a large number of methods. These methods will allow you to treat Arrays as stacks, sets queues, dequeues, and FIFO queues.

The push and pop methods add and remove elements from the end of an array. For the pop method, if you do not define a parameter, it will remove the element from the end of the array. If you define a parameter such as pop(2), it will remove 2 elements from the end of the array.

The shift and unshift methods add and remove elements from the head of an array. Combine shift and push, you have a first-in-first-out (FIFO) queue.

The first and last methods return the n entries at the head or end of an array.

arr.push = 1

arr.pop

arr.pop(2)

arr.shift(2)

arr.unshift

array.first(2)

array.last(2)

Hashes

They are sometimes known as associative arrays, maps or dictionaries. They are similar in that they are indexed collections of object references. Hashes goes a bit further by storing two objects, key and an entry stored with that key.

h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine'}

#shortcut from Ruby 1.9 onwards. if keys are symbols, remove =>

h = {:dog 'canine', :cat 'feline', :donkey 'asinine'}

Compared with arrays, hashes has an advantage. They can use any object as an index. Ruby remembers the order which you add items to a hash. When you iterate the entries, Ruby will return them in that order. Hashes is one of the most commonly used data structures in Ruby.