Open In App

Ruby | Enumerable reverse_each() function

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

The reverse_each() of enumerable is an inbuilt method in Ruby returns the elements of the temporary array. The temporary array contains the enumerable in reverse order. It returns an enumerator if no block is given.

Syntax: enu.reverse_each { |obj| block }

Parameters: The function accepts a block.

Return Value: It returns the elements in reverse order.

Example 1:




# Ruby program for reverse_each method in Enumerable
  
# Initialize 
enu = (1..10)
  
# Prints
enu.reverse_each { |obj| p obj%2==1 }


Output:

false
true
false
true
false
true
false
true
false
true

Example 2:




# Ruby program for reverse_each method in Enumerable
  
# Initialize 
enu = [1, 7, 10, 11]
  
# Prints
enu.reverse_each


Output:

Enumerator: [1, 7, 10, 11]:reverse_each

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads