Ruby | Enumerable reverse_each() function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

false
true
false
true
false
true
false
true
false
true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.