⏎ Return

Arrays and Hashes

In Ruby an array is ordered, integer-indexed collections of any object, arrays are represented as objects. It can hold multiple items called elements of the array. Each element is an array is associated with and referred to by an index. Array indexing starts at 0, for example put name [0] will print out the first name and put name [1] will print out the second name. You can make arrays act like sets, stacks or queues and they do not need to consist of the same data types.

Hashes in ruby are similar to arrays in that they are indexed collection of object references. However while you index arrays with integers, you can index a hash with objects of any type of strings, regular expressions, etc. Hashes enumerate their values in the order that the corresponding keys were inserted. You can store a value in a hash, two objects using a key and the value. Hashes have a default value that is returned when accessing keys that do not exist in the hash, when an attempt is made to access keys that do not exist in the hash it will be known as nil. The main difference between a hash and a array is that compared with arrays, hashes have one significant advantage: they can use any object as an index.