Accessing Arrays
Learn how to use and access arrays in Ruby.
We'll cover the following
Using indexes
So far, we have been iterating over an array. Now, let’s look at how we can access a particular element of an array. We can access the array by using an index. Indexes can be tricky but, at the same time, they’re easy to use. An index can be described as the number of elements minus one. In other words, if we want to access the fifth element of the array, we’ll need an index with a value of 4
. Let’s create an array with five strings in REPL:
Get hands-on with 1400+ tech skills courses.