Skip to content
Related Articles

Related Articles

Ruby | Array reverse() function
  • Last Updated : 06 Dec, 2019

Array#reverse() : reverse() is a Array class method which returns a new array containing self’s elements in reverse order.

Syntax: Array.reverse()

Parameter: Array

Return: a new array containing self’s elements in reverse order.

Example #1 :






# Ruby code for reverse() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [18, 22, 50, 6]
  
# reverse method example
puts "reverse() method form : #{a.reverse()}\n\n"
  
puts "reverse() method form : #{b.reverse()}\n\n"
  
puts "reverse() method form : #{c.reverse()}\n\n"

Output :

reverse() method form : [6, 5, nil, 33, 22, 18]

reverse() method form : [9, 88, 1, 1, 4, 1]

reverse() method form : [6, 50, 22, 18]

Example #2 :




# Ruby code for reverse() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = ["cat", nil]
  
# declaring array
b = ["cow", nil, "dog"]
  
# reverse method example
puts "reverse() method form : #{a.reverse()}\n\n"
  
puts "reverse() method form : #{b.reverse()}\n\n"
  
puts "reverse() method form : #{c.reverse()}\n\n"

Output :

reverse() method form : ["dog", "nil", "abc"]

reverse() method form : ["dog", nil, "cow"]

reverse() method form : [nil, "cat"]

My Personal Notes arrow_drop_up
Recommended Articles
Page :