×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Array reverse! Method

By IncludeHelp Last updated : November 22, 2024

In this article, we will study about Array.reverse! method. You all must be thinking the method must be doing something related to reversing certain elements as we have done in the case of Array.reverse! method. 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.

Description and Usage

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 destructive method where the changes created by this method would impact the actual order of the Self Array instance and these changes are permanent.

Syntax

Array_instance.reverse! -> new_array

Parameters

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: Kotlin
Array elements are:
["Kotlin", "Ruby", "php", "Javascript", "Html", "Python", "Java", "C++"]

Explanation

In the above code, you can observe that we are reversing the contents of Array class instance with the help of Array.reverse! method. You can observe that after reversing the contents, the first element is "Kotlin" because this method is a destructive method and creates 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: 20
Array elements are:
[20, 18, 16, 14, 12, 10, 8, 6, 4, 2]

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 after reversing the contents, the first element is 20 because this method is destructive and creates changes in the actual arrangements of contents in the Array instance.

Comments and Discussions!

Load comments ↻





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