Say, We have the Array with three values, 5, John and Ruby. And we want to access the second element i.e. John.
x = [5, "John", "Ruby"] puts "The second element is : #{x[1]}"
So, in the above code we have created a Array and initialised to the variable x.
x = [5, "John", "Ruby"]
Now, let us see, how the values are positioned in the Array
So, as we can see the elements are positioned as 0, 1 and 2. And if we want to access the second element, we can refer to the position 1 using the square brackets (i.e. x[1]).
And the puts statement prints the value of the second element of the Array (i.e. John).
puts "The second element is : #{x[1]}"
So, we have seen how to access the Array elements using positive Indexes (i.e. 0, 1, 2)
Well! Elements in the Array can also be accessed using negative indexes.
We have the Array with three values, 5, John and Ruby. And we want to access the second element i.e. John.
x = [5, "John", "Ruby"] puts x[-2]
So, in the above code we have created a Array and initialised to the variable x.
x = [5, "John", "Ruby"]
Now, let us see, how the values are positioned in the Array
So, as we can see the elements are positioned as -1, -2 and -3 from backward direction.
And if we want to access the second element using negative index, we can refer to the position -2 using the square brackets (i.e. x[-2]).
And the print statement prints the value of the second element of the Array (i.e. John).
puts x[-2]
We have the Array with three values, 5, John and Ruby. And we want to access the second element i.e. John.
x = [5, "John", "Ruby"] puts x.at(1)
So, in the above code we have created a Array and initialised to the variable x.
x = [5, "John", "Ruby"]
And the puts statement, we have used the at() method and passed the index(i.e. 1) to it.
puts x.at(1)
And it prints the second element (i.e. John).
We have the Array with three values, 5, John and Ruby. And we want to access the 5th element.
Well! Ideally, the 5th element doesn't exist. And if we try accessing the 5th location using the ways we have seen(i.e. Using [] and at() method). It doesn't give us any error.
x = [5, "John", "Ruby"] puts x[4] puts x.at(4)
So if you see the output, you won't be finding any output. That's obvious, right?
Because we are trying to access an element at the 5th location that literally doesn't exist.
puts x[4] puts x.at(4)
But don't you think an Error should be thrown at least. Well! There is no error thrown for [] and at() method.
Now, if you want an error to be thrown for trying to access a location that doesn't exist. You need to use the fetch() method.
x = [5, "John", "Ruby"] puts x.fetch(4)
Well! We got the error for trying to access the 5th location that doesn't exist.
And the great thing about fetch() method is that you can specify the Error message.
x = [5, "John", "Ruby"] puts x.fetch(4,"The 5th element doesn't exist")
So, we got the custom error message that the 5th element doesn't exist.
x = [5, "John", "Ruby"] puts x.first puts x.last
So, we have used first with the array x, to access the first element(i.e. 5).
puts x.first
And used last with the array x, to access the last element(i.e. Ruby).
puts x.last
Now, if you see the output, you can find the elements 5 and Ruby.
x = [5, "John", "Ruby"] puts x.take(2)
So, we have used the take() method to access the first two elements of the Array.
puts x.take(2)
As we can see, the first two elements of the array are, 5 and John. And that's exactly what we got as output.
Say, we want to access the elements after skipping the first record. i.e. We don't want to display the first element 5 and print all the other elements.
x = [5, "John", "Ruby"] puts x.drop(2)
So, we have used the drop() method to skip the first element(i.e. 5) of the Array.
puts x.drop(2)
And display all other elements. And that's exactly what happens, printing John and Ruby as output.
We have the Array with five values, Mohan, John, Paul, Kriti and Salim. And we want to access the second, third and fourth element i.e. John, Paul and Kriti.
x = ["Mohan", "John", "Paul", "Kriti", "Salim"] puts x[1,3]
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "John", "Paul", "Kriti", "Salim"]
Now, let us see, how the values are positioned in the Array
So, as we can see the elements are positioned as 0, 1, 2, 3 and 4.
Now, if we want to access the second, third and fourth element (i.e. John, Paul and Kriti). We can specify the range using ,.
i.e. x[1,3]
x[1,3] actually tells, to pick the elements from index/position 1 to the index/position 3.
And the print statement prints the second, third and fourth element (i.e. John, Paul and Kriti).
puts x[1,3]
So, we have the Array with five values, Mohan, John, Paul, Kriti and Salim. And we want to access the second, third and fourth element i.e. John, Paul and Kriti.
x = ["Mohan", "John", "Paul", "Kriti", "Salim"] puts x[1..3]
So, the above code is self explanatory.
We have used x[1..3] to access the the second, third and fourth element i.e. John, Paul and Kriti.
x[1..3]