Home » Ruby programming

Ruby reverse! function

Ruby reverse! function: Here, we are going to learn about the reverse! function with example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on September 02, 2019

reverse! function in Ruby

As the name suggests, reverse! function is used to reverse the elements of an array. Most of the times, we need to reverse an array but if we do it with the help of loops, the program will become quite lengthy and time-consuming. Ruby facilitates you with reverse! function which produces the reverse of the array. Unlike, reverse function in Ruby, reverse! produces changes in the original array itself. It also returns the reverse of the array which can be stored in the corresponding new array for the fulfillment of future operations.

Syntax:

 Array_name.reverse!

Now, let us understand the implementation concept in a much broader way with the help of program codes.

Example 1:

=begin
Ruby program to demonstrate implementation of reverse! function
=end

# Initializing some arrays of elements 
Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] 
Arr2 = ["one","two", "three", "four", "five"] 
Arr3 = [10,20] 
Arr4 = [110, 210, 310, 410, 510] 
Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] 

# Invoking reverse! function 
NewArr1 = Arr1.reverse!
NewArr2 = Arr2.reverse!
NewArr3 = Arr3.reverse!
NewArr4 = Arr4.reverse! 
NewArr5 = Arr5.reverse!
NewArr6 = Arr6.reverse!

# Printing the the corresponding arrays 
puts "#{NewArr1}"
puts "#{NewArr2}"
puts "#{NewArr3}"
puts "#{NewArr4}"
puts "#{NewArr5}"
puts "#{NewArr6}"

Output

["cherry", "plum", "apricot", "banana", "mango", "apple"]
["five", "four", "three", "two", "one"]
[20, 10]
[510, 410, 310, 210, 110]
["Python", "Perl", "Visual Basic", "C#", "Java", "C++"]
["Wamp", "Kajal", "Amisha", "Satyam", "Hrithik"]

Code logic:

In the above code, we have initialized six arrays. We are reversing them with the help of reverse! statement and storing the returned array in a new corresponding array. Eventually, we are printing the corresponding new arrays.

Now, let us see verify whether reverse! function produces changes in the original array or not.

Example 2:

=begin
Ruby program to demonstrate implementation of reverse! function
=end

# Initializing some arrays of elements 
Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] 
Arr2 = ["one","two", "three", "four", "five"] 
Arr3 = [10,20] 
Arr4 = [110, 210, 310, 410, 510] 
Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] 

# Invoking reverse! function 
NewArr1 = Arr1.reverse!
NewArr2 = Arr2.reverse!
NewArr3 = Arr3.reverse!
NewArr4 = Arr4.reverse! 
NewArr5 = Arr5.reverse!
NewArr6 = Arr6.reverse!

# Printing the the corresponding arrays 
puts "#{Arr1}"
puts "#{Arr2}"
puts "#{Arr3}"
puts "#{Arr4}"
puts "#{Arr5}"
puts "#{Arr6}"

Output

["cherry", "plum", "apricot", "banana", "mango", "apple"]
["five", "four", "three", "two", "one"]
[20, 10]
[510, 410, 310, 210, 110]
["Python", "Perl", "Visual Basic", "C#", "Java", "C++"]
["Wamp", "Kajal", "Amisha", "Satyam", "Hrithik"]

Code Logic:

In the above code, we have tried to show that reverse! function produces changes in the original array as well with the help of ‘puts’ statement through which we are printing the arrays.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.