Difference between revisions of "Advanced Ruby Arrays"
(→Intersection, Union and Difference) |
(→Modifying Arrays) |
||
Line 178: | Line 178: | ||
colors | colors | ||
=> ["red", "orange", "pink"] | => ["red", "orange", "pink"] | ||
− | |||
− | |||
− | |||
− | |||
</pre> | </pre> |
Revision as of 01:36, 27 November 2007
In the Understanding Ruby Arrays we looked at the basics of arrays in Ruby. In this chapter we will look at arrays in more detail.
Combining Ruby Arrays
Arrays in Ruby can be concatenated using a number of different approaches. One option is to simply add' them together:
days1 = ["Mon", "Tue", "Wed"] days2 = ["Thu", "Fri", "Sat", "Sun"] days = days1 + days2 => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
Alternatively, the concat method may be called:
days1 = ["Mon", "Tue", "Wed"] days2 = ["Thu", "Fri", "Sat", "Sun"] days = days1.concat(days2)
Elements may also be appended to an existing array using the << method. For example:
days1 = ["Mon", "Tue", "Wed"] days1 << "Thu" << "Fri" << "Sat" << "Sun" => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
Intersection, Union and Difference
Ruby's support for array manipulation goes beyond that offered by many other scripting languages. One area where this is particularly true involves the ability to create new arrays based on the union, intersection and difference of two arrays. These features are provided via the following set operation symbols:
Operator | Description |
---|---|
- | Difference - Creates a new array from two existing arrays with duplicate elements removed |
& | Intersection - Creates a new array from two existing arrays containing only elements that are common to both arrays. Duplicates are removed. |
| | Union - Concatenates two arrays after removing duplicates |
A few examples will help to clarify these operations. Lets begin by creating two arrays:
operating_systems = ["Linux", "Fedora", "SuSE", "RHEL", "Windows", "MacOS"] linux_systems = ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
Now, we can try performing a union of the two arrays:
operating_systems | linux_systems => ["Linux", "Fedora", "SuSE", "RHEL", "Windows", "MacOS", "PCLinuxOS", "Ubuntu"]
As we can see from the above output, the union operation joined array to another, but removed any duplicate array elements.
Next we can perform an intersection:
operating_systems & linux_systems => ["Fedora", "SuSE", "RHEL"]
This time, we only get elements that common to both arrays.
Finally, we can try a "difference" operation:
operating_systems - linux_systems => ["Linux", "Windows", "MacOS"]
In this case, the new array only contains elements that are not present (ie duplicated) in both arrays.
Identifying Unique Array Elements
The uniq method of the Array class can be used to remove duplicated elements from an array. For example:
linux_systems = ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora", "RHEL", "SuSE"] linux_systems.uniq => ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
Note that in this instance, the original array is unchanged by the uniq method. if you really wanted to strip out the duplicates from an array such that the array itself is altered, use the uniq! method as follows:
linux_systems => ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora", "RHEL", "SuSE"] linux_systems.uniq! => ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"] linux_systems => ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
Push and Popping Array Elements
An array in Ruby can be treated as a last in first out stack where items are pushed onto and popped off the array. This is achieved using the push and pop methods.
For example we can create an array and then push elements onto it:
colors = ["red", "green", "blue"] => ["red", "green", "blue"] colors.push "indigo" => ["red", "green", "blue", "indigo"] colors.push "violet" => ["red", "green", "blue", "indigo", "violet"]
Using the pop method we can also pop elements from the array:
colors.pop => "violet" colors.pop => "indigo"
Note how the elements are popped out of the array, starting with the last to be pushed onto it. Hence the term Last In First Out (LILO).
Ruby Array Comparisons
Ruby arrays may be compared using the ==, <=> and eql? methods.
The == method returns true if two arrays contain the same number of elements and the same contents for each corresponding element.
The eql? method is similar to the == method with the exception that the values in corresponding elements are of the same value type.
Finally, the compares two arrays and returns 0 if the arrays are equal, -1 one if the elements are less than those in the other array, and 1 if they are greater.
Modifying Arrays
A new element may be inserted into an array using the insert method. This method takes as arguments the index value of the element to be inserted, followed by the new value. For example, to insert a new color between the red and green elements:
colors = ["red", "green", "blue"] => ["red", "green", "blue"] colors.insert( 1, "orange" ) => ["red", "orange", "green", "blue"]
Any array element may be changed simply by assigning a new value using the array element index:
colors = ["red", "green", "blue"] => ["red", "green", "blue"]=> ["red", "yellow", "blue"] colors[1] = "yellow" => "yellow" colors => ["red", "yellow", "blue"]
Multiple elements can be changed by making use of a range:
colors = ["red", "green", "blue"] => ["red", "green", "blue"] colors[1..2] = "orange", "pink" => ["orange", "pink"] colors colors => ["red", "orange", "pink"]