Collections In Ruby On Rails

We will see some of the collections used in Ruby on Rails with their syntaxes and example programs to help you get a better understanding of this language.

 

CONTENT

  • Adding Elements.
  • Removing Elements.
  • Array Ranges.
    • Array Manipulations.
  • Combining Arrays.
  • Boolean Operations.
  • Moving Elements.
  • Array Methods.

Arrays

 
An array is essentially a lot of numbered items. The items are declared in a comma-separated list and enclosed in square brackets.

Example

items = [“Apple”, “Orange”, “Banana”]

The code above declares an array named "items" containing three strings. Each item has its own unique index with the first item at index zero. To refer to an item, we need to refer to its index.

Example

Puts items[0]

You can assign any array element a new value by using its index.

Example

Items[1] = “kiwi”

Ex

Collections Of Ruby On Rails 
 
This will assign the element with index 1 and the value “kiwi”.
 
Adding Elements
 
An array can contain different types of elements.

Ex

Collections Of Ruby On Rails 

NOTE
A negative index is assumed relative to the end of the array. For example, an index of -1 indicates the last element of the array, -2 is the next to last element in the array.

To add new elements to the array, you can use the << operator, which is typed as two less than signs.

Ex

Collections Of Ruby On Rails 
 
This will add an element with the value 10 to the end of the array. Alternatively, you can use the "push" and "insert" methods.

Syntax

arr.push(8)

Ex

Collections Of Ruby On Rails 
 
This will add 8 to the end of the array. The insert method allows you to insert the element at the desired position.

Syntax

arr.insert(2,8)

Ex

Collections Of Ruby On Rails 
 
The code above will insert an element with value 8 at the position with index 2.
 
Removing Elements
  • Similarly, there are pop and delete_at methods available to remove the elements from the array.
  • The pop method removes the last element of the array.

Ex

Collections Of Ruby On Rails 
 
When used to output arrays, the puts method outputs one element per line, while print actually outputs brackets and a list of items.
 
You can use the delete_at method to specify the index of the element to be removed.

Ex

Collections Of Ruby On Rails 
 
The index starts counting at 0. So, the third element in the array has the index 2.
 
Array Ranges
 
You can even access a range within the array using the range operators.

Ex

Collections Of Ruby On Rails 

Array Manipulations

 
Combining Arrays
 
You can add two arrays together.

Ex

Collections Of Ruby On Rails 
 
You can also subtract arrays, which will result in the first array removing any element that also appears in the second array.
 
Boolean Operations
 
The & operator returns a new array containing the elements common to the two arrays with no duplicates.

Ex

Collections Of Ruby On Rails 
 
The | operator returns a new array by joining the arrays and removing duplicates.

Ex

Collections Of Ruby On Rails 
 
The operators mentioned above are not the same as (&&) and ( || ).
 
Moving Elements

The reverse method returns a new array containing the original array element in a reverse order.

Syntax

arr.reverse

Ex

Collections Of Ruby On Rails 
 
You can also reverse the array in place using the reverse!

Ex

Collections Of Ruby On Rails 
 
If you use the reverse method without (!) and don’t save it to a new variable, it will just reverse it once and keep the original value.
 
Array Methods
  • There are a number of other useful methods available for manipulating arrays.
  • Here are some of the most used ones,

    • length or array.size returns the number of element in the array.
    • sort a new array with the elements sorted.
    • uniq returns a new array with the duplicate values removed from the array.
    • uniq! removes duplicates in place.
    • freeze safeguards the array, preventing it from being modified.
    • include? (obj) returns true if an object is present in the array, false otherwise.
    • min returns the element with the minimum value.
    • max returns the element with the maximum value.

  • Most of the methods also work for strings, which can be thought of as arrays of Character. A for loop is one way to iterate over an array of elements.

Ex

Collections Of Ruby On Rails 
 
This will loop through all the elements of the array and output them per line.
 

Summary

Today we learned some important Collections in Ruby. In my upcoming article, I will continue with this topic. If you have any queries, please ask me.


Similar Articles