Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - ACCESSING ARRAY


Accessing the elements of the 'Array' using positive Indexes


Say, We have the Array with three values, 5, John and Ruby. And we want to access the second element i.e. John.


Example :



x = [5, "John", "Ruby"]
puts "The second element is : #{x[1]}" 


Output :



  The second element is : John

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

java_Collections

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.


Accessing the elements of the 'Array' 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.


Example :



x = [5, "John", "Ruby"]
puts x[-2] 


Output :



  John

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

java_Collections

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]

Accessing the elements of the 'Array' using 'at()' method


We have the Array with three values, 5, John and Ruby. And we want to access the second element i.e. John.


Example :



x = [5, "John", "Ruby"]
puts x.at(1) 


Output :



  John

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


Accessing the elements of the 'Array' using 'fetch()' method


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.


Example :



x = [5, "John", "Ruby"]
puts x[4]
puts x.at(4) 


Output :





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.


Example :



x = [5, "John", "Ruby"]
puts x.fetch(4) 


Output :



  in fetch: index 4 outside of array bounds: -3...3 (IndexError)
  from add.rb:2:in <main>

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.


Example :



x = [5, "John", "Ruby"]
puts x.fetch(4,"The 5th element doesn't exist") 


Output :



  The 5th element doesn't exist

So, we got the custom error message that the 5th element doesn't exist.


Accessing the First and Last elements of the Array


Example :



x = [5, "John", "Ruby"]
puts x.first
puts x.last


Output :



  5
  Ruby

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.


Accessing the First few elements of the Array using 'take()' method


Example :



x = [5, "John", "Ruby"]
puts x.take(2)


Output :



  5
  John

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.


Accessing the elements of the Array after skipping few records using 'drop()' method


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.


Example :



x = [5, "John", "Ruby"]
puts x.drop(2)


Output :



  John
  Ruby

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.


Accessing a chunk of elements of the 'Array' using range of Indexes


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.


Example :



x = ["Mohan", "John", "Paul", "Kriti", "Salim"]
puts x[1,3]


Output :



  John
  Paul
  Kriti

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

java_Collections

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.

java_Collections

And the print statement prints the second, third and fourth element (i.e. John, Paul and Kriti).


puts x[1,3]

Output :



  John
  Paul
  Kriti

Accessing a chunk of elements of the 'Array' using '..'


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.


Example :



x = ["Mohan", "John", "Paul", "Kriti", "Salim"]
puts x[1..3]


Output :



  John
  Paul
  Kriti

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]