When you’re programming, you’ll use and encounter various data structures including arrays and hashes. Eventually, you’ll want to iterate through each item in an array or hash to extract or transform the information in these data structures. The ability to transverse through an object is called enumeration.
Ruby has several built-in methods to go through each item of an array or hash and return the information you need. These built-in enumerates include methods like `map`, `uniq`, `include?`, as well as several others. In this article, we will discuss how to use the enumerable `each_with_index` as well as the difference between `each_with_index` and `each.with_index()`.
Each with Index
Each with Index does what the name indicates: it iterates through each element in an array or hash, and extracts the element, as well as the index (the element’s place in the array) and will transform both the element and its index based on the code you have written.
The syntax is:
array.each_with_index do | element, index | puts "#{element} is number #{index} in the array" end
Here’s another example:
Let’s say we have an array of the top ten Fortune 500 Companies from 2020, and we wanted to output them in a string with their rank. We might write some code that looked like this:
top_10 = ["Walmart", "Exxon Mobil", "Apple", "Berkshire Hathaway", "Amazon.com", "UnitedHealth Group", "McKesson", "CVS Health", "AT&T", "AmerisourceBergen"] top_10.each_with_index do | company, index | puts "#{index}. #{company}" end
The output of our code would be:
0. Walmart
1. Exxon Mobil
2. Apple
3. Berkshire Hathaway
4. Amazon.com
5. UnitedHealth Group
6. McKesson
7. CVS Health
8. AT&T
9. AmerisourceBergen
Here we see the index puts the index number followed by the company name. The issue here is that the first item in an array has an index of 0. But for our purposes, we would like to start the list with the number one.
each.with_index()
A second option is to use each.with_index
instead of each_with_index
. The two methods look very similar, but each.with_index
takes in an optional argument of where to start the count.
If you do not include an argument, the index will start at 0. If you would like your index to start with a different number, you would write array.each.with_index(integer)
. Again, this does not change the index of an element in the array, it only changes the output.
Here’s how we applied it to our code:
top_10 = ["Walmart", "Exxon Mobil", "Apple", "Berkshire Hathaway", "Amazon.com", "UnitedHealth Group", "McKesson", "CVS Health", "AT&T", "AmerisourceBergen"] top_10.each.with_index(1) do | company, index | puts "#{index}. #{company}" end
The output of our code would be:
1. Walmart
2. Exxon Mobil
3. Apple
4. Berkshire Hathaway
5. Amazon.com
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
6. UnitedHealth Group
7. McKesson
8. CVS Health
9. AT&T
10. AmerisourceBergen
It worked again!
Conclusion
In this article we learned how to use Ruby’s enumerable `each_with_index` as well as how to circumvent the index starting with the number zero, using `each.with_index()`. You can find more on other Ruby Array Methods here. If you want to learn more about what you can build with Ruby, check out our article, ”What Is Ruby Code Used For?”
Let Career Karma help you find the best way to start your career making web applications with Ruby.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.