Wednesday, 21 May 2014

Ruby - Arrays (and an overview of %w or %W)

Ruby arrays are a step up from using Variables to store single pieces of data. As you’ve no doubt guessed by their name, Arrays store collections of data elements. Just like Variables, we can set-up many Arrays to store specific collections of data. This could be anything such as customer names, attributes for something such as size and colour, it could be payment dates for a payroll run, the ingredients for Jam or components for an intergalactic spaceship!

OK, at some point you might want to switch to storing that data in a database and just using Arrays to store working data, but you get the idea. Arrays allow us to store much larger, related sets of data than we could do with Variables.

Let’s jump into it…


Creating an Array

To create a new array we provide a name for the array and use a pair of square brackets, within which we specify our data set or ‘tokens’ as you’ll sometimes see the item referred to.

myCityArray1 = ["London", "Milan", "New York", "Madrid", "Paris", "Shanghai"]

That’s the most straightforward form of array, you can use single quotes too. Instead of writing out our array above, we can use the whitespace between values as the delimiter. Note that where you data has a space in it, such as in ‘New York’, you will need to escape the space using  \ before the space, making it New\ York. To use we use the % symbol to create the array and separate items using lowercase w to specific the whitespace as the delimiter.

myCityArray2 = %w[London Milan New\ York Madrid Paris]

It’s not a huge timesaver when writing them out manually, but does avoid a lot of key strokes. It also avoids the mistake I made in the first array above, did you spot it? I’m on the fence as to which approach is better, but you might encounter it so it’s all good to know.

- Uppercase W
You may have also encountered an uppercase W being used in this way;

myCityArray3 = %W[London Milan New\ York Madrid Paris]

What’s the difference? For one, we can use Interpolation with uppercase W, but not with lowercase. Let’s try a new array with data items of  #{1+2}   and  a  and 2  , remember the single spaces:

myMathArray = %w[#{1+2} a 2]
puts myMathArray [0]

What was the output, #{1+2} or 3? Now change it to an uppercase W, how about now?
With uppercase W, interpolation works.

You’ll notice that to access an element in an array, we’re using square brackets with the position in. The above example showed us using [0] to get the first element.

More on data in Arrays

An array can of course contain more than just strings, it can have integers and floats.

myMixedArray = ["Three", 2, 1.0]

If we don’t know what data will go in the array when we construct it, we can declare an empty array using the New method with no arguments.

myEmptyArray =

This can be useful where we know data will need to be held, but a user or system hasn’t provided it to us yet. You can also set the initial size of the array (which can be modified later), where perhaps you know the current size of the data set you may want to populate the array with later.

mySizedArray =

If wanted, you can even set-up a multi-dimensional array, here’s a 2D array example.

myMultiArray = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

To access the elements in a multi-dimensional array we need to specify which position of our ‘outer’ array we’re interested in, then likewise the ‘inner’. For example, to get to the value 4, remembering to count from 0, we’d use:

puts myMultiArray [1][1]

We’re just indexing in, then in again as we do for any normal array. There is a way to index in from the end, backwards too. Using the example from before, let’s index into Madrid.

myCityArray4 = ["London", "Milan", "New York", "Madrid", "Shanghai"]
puts myCityArray5 [-2]

Slightly confusingly, we count from 1 not 0 when going backwards. No, I don’t get it either but just roll with it!

These simple indexing methods are good to use where you want an element from a known position. However, sometimes we need to understand what data is in the array in order to use it.

That's the basics of Ruby Arrays.

Have a look at using these with Variables and using Interpolation to pull in Variable values.