Arrays and Hashes

~ Written as part of my coursework at Dev Bootcamp ~

December 30, 2015

Ruby Arrays

An Array is a data structure. It eases the manipulation of data within a pogram. The data in an array is stored by index.

An array can be declared like this: array =[1, 2, 3, 'four', 'five', 'six']

Since the data is arranged by index, you can access the elements with calling the index position within brackets: array[0] would access the first element in the array. array[2] would access the third eleemnt. A negative index can be used to access elements starting from the end.

Ruby Hashes

A Hash is also a data structure. It also eases the manipulation of data within a pogram. The data in a hash is stored by an index of integers. The index begins at 0.

A Hash can be created much like an Array, declaring it like so: hash = { :apples => 10, :oranges => 8, :pears => 9}.

The elements within a hash are not organized by index or anything else that would allow accessing them by position. However, the elements within a hash include key value pairs, so the elements can be accessed by calling upon the keys: hash[:apples].

Both hashes and arrays each have many methods that allow developers to transform the data within arrays and hashes, no matter how large the arrays or hashes may be, and for all different forms of data that may be contained.