Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Collections in Ruby 
2.1.
Enumerable Objects
2.2.
Arrays 
2.3.
Hashes
2.4.
Sets
3.
Frequently Asked Questions
3.1.
What is Ruby?
3.2.
What are collections in Ruby?
3.3.
What is an Array in Ruby collections?
3.4.
What is a set in Ruby collections?
3.5.
What is a Hash object in Ruby?
4.
Conclusion
Last Updated: Mar 27, 2024

Collections in Ruby

Author Teesha Goyal
0 upvote

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.

ruby logo

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. 

collections in ruby

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 ([ ]).

Array in ruby

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). 

Hash in ruby

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.  

sets in ruby

 

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. 

Frequently Asked Questions

What is Ruby?

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.

What are 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.

What is an Array in Ruby collections?

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 ([ ]).

What is a set in Ruby collections?

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 ({ }). 

What is a Hash object in Ruby?

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 ({ }).

Conclusion

This article discussed collections in Ruby. We discussed Enumerable objects, ArraysHashes, and sets in Ruby.

I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Thank You

Happy Coding!

Live masterclass