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




RUBY - REVERSE A ARRAY


How to reverse a Array?


Reversal of a Array can be done using reverse() method. It is independent of the alphabets. And is not a sort. It is just a reversal.


Example :



x = ["Mohan", "Kriti", "Salim"] 
y = x.reverse()
puts "The Array in reversed order is #{y}" 


Output :



  The Array in reversed order is ['Salim', 'Kriti', 'Mohan']

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Then we have used the reverse() Method to reverse the elements of the Array x.


y = x.reverse()

And the new Array y has the items in reversed order with Salim as the first value, Mohan second and Kriti as the third.

java_Collections

And we get the below output.


The Array in reversed order is  ['Salim', 'Kriti', 'Mohan']