Home » Ruby programming

Array.reverse Method with Example in Ruby

Ruby Array.reverse Method: Here, we are going to learn about the Array.reverse method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 04, 2020

Array.reverse Method

In this article, we will study about Array.reverse method. You all must be thinking the method must be doing something which is related to reversing certain elements. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that reverses the content or objects present inside the Array instances. It traverses the Array elements in the way that the last element is considered to be first and the first object is considered to be the last element of the object of Array instance. Array.reverse is a non-destructive method where the changes created by this method would not impact the actual order of the Self Array instance.

Syntax:

    array_instance.reverse -> new_array

Argument(s) required:

This method does not require any argument. Reversing the Array instance does not require anything rather than the Array instance itself.

Example 1:

=begin
  Ruby program to demonstrate reverse method
=end

# array declaration
lang = ["C++","Java","Python","Html","Javascript","php","Ruby","Kotlin"]

puts "Array reverse implementation."
print lang.reverse
puts ""

puts "The first element of the Array is: #{lang[0]}"
puts "Array elements are:"
print lang

Output

Array reverse implementation.
["Kotlin", "Ruby", "php", "Javascript", "Html", "Python", "Java", "C++"]
The first element of the Array is: C++
Array elements are:
["C++", "Java", "Python", "Html", "Javascript", "php", "Ruby", "Kotlin"]

Explanation:

In the above code, you can observe that we are reversing the contents of the Array class instance with the help of the Array.reverse method. You can observe that even after reversing the contents, the first element is still "C++" because this method is a non-destructive method and does not create any changes in the actual arrangements of contents in Array instance.

Example 2:

=begin
  Ruby program to demonstrate reverse method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array reverse implementation"
print table.reverse
puts ""

puts "The first element of the Array is: #{table.first}"
puts "Array elements are:"
print table

Output

Array reverse implementation
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
The first element of the Array is: 2
Array elements are:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Explanation:

In the above code, you can observe that this method works on Integer Array as well and we are reversing the contents of Array class instance with the help of Array.reverse method. You can observe that even after reversing the contents, the first element is still 2 because this method is non-destructive and does not create any changes in the actual arrangements of contents in the Array instance.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.