Changes

Advanced Ruby Arrays

3,979 bytes added, 01:35, 27 November 2007
Intersection, Union and Difference
<td align="center">|</td><td>Union - Concatenates two arrays after removing duplicates</td>
</table>
 
A few examples will help to clarify these operations. Lets begin by creating two arrays:
 
<pre>
operating_systems = ["Linux", "Fedora", "SuSE", "RHEL", "Windows", "MacOS"]
 
linux_systems = ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
</pre>
 
Now, we can try performing a union of the two arrays:
 
<pre>
operating_systems | linux_systems
=> ["Linux", "Fedora", "SuSE", "RHEL", "Windows", "MacOS", "PCLinuxOS", "Ubuntu"]
</pre>
 
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:
 
<pre>
operating_systems & linux_systems
=> ["Fedora", "SuSE", "RHEL"]
</pre>
 
This time, we only get elements that common to both arrays.
 
Finally, we can try a "difference" operation:
 
<pre>
operating_systems - linux_systems
=> ["Linux", "Windows", "MacOS"]
</pre>
 
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:
 
<pre>
linux_systems = ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora", "RHEL", "SuSE"]
 
linux_systems.uniq
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
</pre>
 
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:
 
<pre>
linux_systems
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora", "RHEL", "SuSE"]
 
linux_systems.uniq!
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
 
linux_systems
=> ["RHEL", "SuSE", "PCLinuxOS", "Ubuntu", "Fedora"]
</pre>
 
== 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:
 
<pre>
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
 
colors.push "indigo"
=> ["red", "green", "blue", "indigo"]
 
colors.push "violet"
=> ["red", "green", "blue", "indigo", "violet"]
</pre>
 
Using the ''pop'' method we can also pop elements from the array:
 
<pre>
colors.pop
=> "violet"
 
colors.pop
=> "indigo"
</pre>
 
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:
 
<pre>
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
 
colors.insert( 1, "orange" )
=> ["red", "orange", "green", "blue"]
</pre>
 
Any array element may be changed simply by assigning a new value using the array element index:
 
<pre>
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]=> ["red", "yellow", "blue"]
 
colors[1] = "yellow"
=> "yellow"
 
colors
=> ["red", "yellow", "blue"]
</pre>
 
Multiple elements can be changed by making use of a range:
 
<pre>
colors = ["red", "green", "blue"]
=> ["red", "green", "blue"]
 
colors[1..2] = "orange", "pink"
=> ["orange", "pink"]
 
colors
colors
=> ["red", "orange", "pink"]
</pre>
 
 
 
</pre>