3.0 Ripping the Guts

Lining 'em up

You've already completed the basic introduction to Arrays and are ready to delve deeper. This chapter will cover Ruby's Array in some detail, and demonstrate advanced techniques around the creation and manipulation of arrays.

Destructurin'

If you've reached this far on RubyMonk, you've probably come across an expression like this.
Example Code:

Output Window

This is destructuring. We've broken down the array and assigned its values to zen and life.

This is equivalent to using the bracket form or the at method to extract values. giving you nice a shorthand for doing a sequential breakdown of an array.

Example Code:

Output Window

What happens when one has a multi-dimensional array? Multi-dimensional arrays are n-element arrays within an array. That is, each element of the array is also an array of n items.
Example Code:

Output Window

zen and john expectedly select the first two inner-arrays.

It's also possible to write a function and use it in a way as to simulate returning multiple values from it. In practice, this is rarely necessary. But it's fun to know you can!

Example Code:

Output Window

All this is pretty neat. What is even nicer is that you can use this inside blocks.

Example Code:

Output Window

This is equivalent to destructuring them manually inside the block instead of the block arguments.
Example Code:

Output Window

Finish this method to return an array. The method accepts only a two-dimensional array. The elements of the array that this method returns are the sums of the first two elements of each inner-array of the two-dimensional array that is passed in.

Output Window

If you've noticed in the examples above, destructuring an array is slightly restricting.
Example Code:

Output Window

In the exercise above, more is ignored and simply assigned nil because we're out of elements in the array.

The splat

Ruby has a more dedicated way of destructuring using the splat (*) operator.

Example Code:

Output Window

This is different from our regular way of destructuring by assignment as instead of just splitting the array up into the number of variables present on the left hand side, we're splitting the array up by the number of variables and slurping the rest of the array into the second variable with the splat (*).

This example should make this more clear.

Example Code:

Output Window

initial here only slurps the elements before 44. This is because we have two variables and last takes away the final value.

What if we add another variable into this?

Example Code:

Output Window

As expected, initial only slurps [42] from the array. The last two values are assigned to the last two variables.

Modify this expression to slurp the middle portion of this array using a middle variable.

Output Window

You can also slurp or collate variable number of arguments passed into a method as an array.
Example Code:

Output Window

You can however only splat the last parameter of a method.

Now write an exercise to calculate the median from a set of numbers. Assuming that median method takes in the list as arguments passed in.

Hint

One must first sort the list of items.

If the number of items (n) in the list is odd, then the median is the middle item ((n + 1) / 2)th item.

If they are even, then the median is the mean of the two items in the middle [(n / 2)th item + ((n / 2) + 1)th item] / 2.

Output Window

The splat isn't restricted to just the left hand side. You can also use it with Range, String and convert them into Array objects.
Example Code:

Output Window

It's also possible to use this left hand side form to turn array items into method arguments.
Example Code:

Output Window

You can of course, use this with blocks as well.
Example Code:

Output Window

a is the first element and b is an array containing the rest of the elements.

Hash is often created using the array form that takes in even number of arguments as key-value pairs, or directly, a two-dimensional array with paired arrays.

Example Code:

Output Window

You can use the splat in this form to create hashes.
Example Code:

Output Window

We're just getting started. We'll take a look at some of the nice methods that Array class provides in the next lesson.

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Jasim A Basheer