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

In ruby, as the name suggests, reverse function is used to reverse the array but with a twist, it reverses the array and stores it into the new array. The reverse function produces no effect on the original array. By this function, whatever the order of the array is, that order gets reversed.

The reverse function does not permit any parameters. Its return type is an array and obviously, the array returned is the reverse of actual array.

Syntax:

 new_array = old_array.reverse

Now, let us understand the implementation of the reverse function more broadly with the help of examples.

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"] 

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

# Printing the new reversed array 
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"]

In the above example, you can observe that we have initialized six arrays and then we are invoking reverse function inside the new array. reverse function is returning the array in reverse order and storing the values in a new corresponding array.

Now let us find, whether changes made by the reverse function are reflected in the original array or not by taking reference of an example given below,

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"] 

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

# Printing the original array 
puts "#{Arr1}"
puts "#{Arr2}"
puts "#{Arr3}"
puts "#{Arr4}"
puts "#{Arr5}"
puts "#{Arr6}"

Output

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

You can observe the difference between both the outputs. With the help of an example, it is practically proven that reverse function does not affect the original array.



Comments and Discussions!

Load comments ↻





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