Open In App

Ruby | Array reverse() function

Last Updated : 06 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Array#reverse() : reverse() is a Array class method which returns a new array containing self’s elements in reverse order.

Syntax: Array.reverse()

Parameter: Array

Return: a new array containing self’s elements in reverse order.

Example #1 :




# Ruby code for reverse() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [18, 22, 50, 6]
  
# reverse method example
puts "reverse() method form : #{a.reverse()}\n\n"
  
puts "reverse() method form : #{b.reverse()}\n\n"
  
puts "reverse() method form : #{c.reverse()}\n\n"


Output :

reverse() method form : [6, 5, nil, 33, 22, 18]

reverse() method form : [9, 88, 1, 1, 4, 1]

reverse() method form : [6, 50, 22, 18]

Example #2 :




# Ruby code for reverse() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = ["cat", nil]
  
# declaring array
b = ["cow", nil, "dog"]
  
# reverse method example
puts "reverse() method form : #{a.reverse()}\n\n"
  
puts "reverse() method form : #{b.reverse()}\n\n"
  
puts "reverse() method form : #{c.reverse()}\n\n"


Output :

reverse() method form : ["dog", "nil", "abc"]

reverse() method form : ["dog", nil, "cow"]

reverse() method form : [nil, "cat"]



Similar Reads

Ruby | Array reverse!() function
Array#reverse!() : reverse!() is a Array class method which returns reverses self in place Syntax: Array.reverse!() Parameter: Array Return: Reverses self in place Example #1 : # Ruby code for reverse!() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 6] # reverse! me
1 min read
Ruby | reverse function
The reverse function in Ruby is used to reverse the input array into another new array and keep the input array as it is before. 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 another new array of reversed elements of the input array. Exam
2 min read
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: C/C++ Code # Initializing some arrays of elements Array1
2 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | String reverse Method
reverse is a String class method in Ruby which is used to return a new string with the characters from the given string in reverse order. Syntax: str.reverse Parameters: Here, str is the string which is to be reversed. Returns: This method returns a new string in reversed order. Example 1: # Ruby program to demonstrate # the reverse method # Taking
1 min read
Ruby | Array Concatenation using (+) function
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array. Syntax: Array.+() Parameter: Arrays for performing the concatenation operation. Return: New arrays by combining two arrays. Example #1 : # Ruby code for +() method # showing concatenate operation # declaring array a = [18, 22,
2 min read
Ruby | Array assoc() function
The assoc() function in Ruby is used to search through an array of arrays whose first element is compared with the index of the function and return the contained array if match found otherwise return either nil or vacant. Syntax: Array.assoc(Object) Here Array is the array of arrays. Parameters: Object : It is an element which gets compared with th
2 min read
Ruby | Array abbrev() function
abbrev() is an Array class method which provides unambiguous set of abbreviations for the string. Syntax: Array.abbrev()Parameter:- string for abbreviation - pattern [optional]Return: unambiguous abbreviations set Example #1 : Example for abbrev() method C/C++ Code # Ruby code for abbrev() method # checking for unambiguous abbreviations require 'ab
1 min read
Ruby Array () function with example
Array#() : () is an Array class method which performs the comparison between the two arrays. Syntax: Array.() Parameter: Array for the comparison Return: -1 : array is less than the other array 0 : array is equal to other array 1 : array is greater than the other array Example #1 : # Ruby code for <=>() method # showing comparison # declaring
2 min read
Ruby | Array class first() function
first() is a Array class method which returns the first element of the array or the first 'n' elements from the array. Syntax: Array.first() Parameter: Array n - no. of elements Return: first element of the array or the first 'n' elements from the array Example #1 : # Ruby code for first() method # declaring array a = [18, 22, 33, nil, 5, 6] # decl
1 min read