Introduction
Ruby is a dynamic, open-source, object-oriented, and interpreted programming language that can be used for web scraping, building desktop applications, web servers, DevOps, and much more. It is a beginner-friendly language with easy-to-understand syntax.
This article will discuss collections in Ruby. We will discuss enumerable, array, hash, and sets in Ruby.
Collections in Ruby
A Ruby collection is any class representing a collection/set of values or elements. Any class that can provide a mechanism to store more than one element is called a collection. The two key collections in Ruby are Array and Hash, and the standard library adds another one called a Set. All three collections work with the enumerable module making them universal collection methods.
Enumerable Objects
Array, Hash, and Set come under enumerable objects because all the enumerable methods can be applied to these collections. Each one of these is explained in the later sections of the article. Other than these, Range and IO are the two main enumerable objects.
Following are some of the essential enumerable methods in Ruby:
-
Iterating and converting collections: Traversing/iterating is a vital operation on any collection or enumerable object.
-
Enumerators and external iterators: Enumerators are other iterators used where we don’t have a collection but need to iterate or perform something a fixed number of times.
-
Sorting collection: Sorting is one of the most important methods. It is used to rearrange the elements of a collection systematically (either in an ascending or descending manner).
-
Searching collections: Search whether or not an element is present in any collection.
-
Find in collections: Find or detect is a method that finds and returns the index of an element if it is present in the collection.
-
Selecting subcollections: Selecting some part of the collection and returning it based on some conditions.
- Reducing collections: The min and max methods reduce the collections to a single element.
Arrays
An array is one of the most fundamental and commonly used collections in Ruby. It is a linear data structure that stores the elements sequentially. Arrays are the comma-separated elements enclosed within a square bracket ([ ]).
An index is assigned to each element of an array. Indexing of array in ruby starts with 0, and the negative indexing starts with -1 from the last element and decreases as we move towards the first element.
Example of an array:
Myarray = [3, 6, 9, 10, 2, 1]
Various methods are present in the array class, making it simple and more effortless to use the array. To learn more about array methods, visit Arrays in Ruby - Coding Ninjas Coding Ninjas Studio.
Hashes
Hash is a collection in Ruby that stores elements in unique key-value pairs. The keys should be unique within a hash object. However, the value can be similar for different keys. Hash is a collection of key-value pairs enclosed within curly brackets ({ }). Indexes in a hash object can be of any object type (character, string).
Example of hash:
Myhash = { one = 1, a = 3 }
Other names for a hash collection are Associative arrays, dictionaries, and maps. To learn more about the various class methods and uses of hash, visit Hashes in Ruby - Coding Ninjas Coding Ninjas Studio.
Sets
Set is yet another collection in ruby similar to an array but with distinct elements. No duplicates are allowed in a set. Elements in a set are unordered. Sets are comma-separated elements enclosed within curly brackets ({ }). If we try to add an element to a set object that already exists, it is simply ignored and not inserted.
Example of set:
Myset = { 2, 4, 6, 8 }
Sets are suitable to perform mathematical set operations (like intersection, union, difference, exor) on a collection of elements. All the other enumerable methods can also be applied to sets.