Ruby | reverse! function
The reverse! function in Ruby is used to reverse the input array into the same array.
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 same input array with reversed element.
Example 1:
# Initializing some arrays of elements Array1 = ["a", "b", "c", "d"] Array2 = [] Array3 = [1] Array4 = [1, 2] Array5 = ["Ram", "Geeta", "Shita"] # Calling reverse! function A = Array1.reverse! B = Array2.reverse! C = Array3.reverse! D = Array4.reverse! E = Array5.reverse! # Printing the same input array # with reversed elements 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 reverse! function A = Array1.reverse! B = Array2.reverse! C = Array3.reverse! D = Array4.reverse! E = Array5.reverse! # Prining original input array # after calling reverse! function puts "#{Array1}"puts "#{Array2}"puts "#{Array3}"puts "#{Array4}"puts "#{Array5}" |
Output:
["d", "c", "b", "a"] [] [1] [2, 1] ["Shita", "Geeta", "Ram"]
Note: Difference between reverse and reverse! functions is that reverse function reverses the input array elements into another array and keep the input array as it is but the reverse! function reverses the input array into the same input array.
Reference: https://devdocs.io/ruby~2.5/array#method-i-reverse-21
Recommended Posts:
- Ruby | reverse function
- Ruby | Array reverse!() function
- Ruby | Array reverse() function
- Ruby | String reverse Method
- Ruby | Set add? function
- Ruby | at() function
- Ruby | pop() function
- Ruby | Numeric zero? function
- Ruby | DateTime sec() function
- Ruby | Set divide() function
- Ruby | DateTime second() function
- Ruby | Complex abs function
- Ruby | Regexp new() function
- Ruby | Time eql?() function
- Ruby | abbrev 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.



