Ruby | reverse function
The reverse function in Ruby is used to reverse the input array into another new array and keep the input array as it is before.
Syntax: Array.reverse
Here Array is the input array whose elements are to be reversed.Parameters: This function does not accept any parameters.
Returns: the another new array of reversed elements of the input array.
Example 1:
# Initializing some arrays of elements Array1 = [ "a" , "b" , "c" , "d" ] Array2 = [] Array3 = [ 1 ] Array4 = [ 1 , 2 ] Array5 = [ "Ram" , "Geeta" , "Shita" ] # Calling to reverse function A = Array1.reverse B = Array2.reverse C = Array3.reverse D = Array4.reverse E = Array5.reverse # Printing the new reversed array puts "#{A}" puts "#{B}" puts "#{C}" puts "#{D}" puts "#{E}" |
Output:
["d", "c", "b", "a"] [] [1] [2, 1] ["Shita", "Geeta", "Ram"]
Example 2:
# Initializing some arrays of elements Array1 = [ "a" , "b" , "c" , "d" ] Array2 = [] Array3 = [ 1 ] Array4 = [ 1 , 2 ] Array5 = [ "Ram" , "Geeta" , "Shita" ] # Calling to reverse function A = Array1.reverse B = Array2.reverse C = Array3.reverse D = Array4.reverse E = Array5.reverse # Printing original input array puts "#{Array1}" puts "#{Array2}" puts "#{Array3}" puts "#{Array4}" puts "#{Array5}" |
Output:
["a", "b", "c", "d"] [] [1] [1, 2] ["Ram", "Geeta", "Shita"]
Note: In the above example, it can be seen that after calling the reverse function it reverse the original input array into another array and keep the original array as it is before.
Reference: https://devdocs.io/ruby~2.5/array#method-i-reverse
Recommended Posts:
- Ruby | reverse! function
- Ruby | Array reverse!() function
- Ruby | Array reverse() function
- Ruby | String reverse Method
- Ruby | at() function
- Ruby | Set add? function
- Ruby | pop() function
- Ruby | Numeric eql?() function
- Ruby | Matrix tr() function
- Ruby | Numeric dup() function
- Ruby | Set empty?() function
- Ruby | Vector eql?() function
- Ruby | Hash each() function
- Ruby | Hash dig() function
- Ruby | Integer << function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.