reverse_each method

The reverse_each method takes an enumerable collection, reverses the order of the elements, and yields one element at a time to the given block.

Number List

@numbers = [1, 0, 3, 2, 5, 4, 7, 6, 9, 8]

# all numbers, backward:
@numbers.reverse_each.entries
    #=> [8, 9, 6, 7, 4, 5, 2, 3, 0, 1]

Pet Inventory

# leg counts for all pets, backward:
@inventory.reverse_each.map(&:legs)
    #=> [0, 2, 6, 8, 0, 4, 4]

# leg totals for each pet type, if all pets have 100 in stock (backward):
@inventory.reverse_each(:quantity => 100){|pet| puts "#{pet.legs * pet.quantity}"}

# output
# 0
# 200
# 600
# 800
# 0
# 400
# 400

Pokey Things

# all pokey things, backward:
@pokey_things.seek(0)
@pokey_things.reverse_each.map(&:chomp)
    #=> ["cactus holding poles with knives attached", "knife", "pole", "cactus"]

Heroku Log File

# all heroku requests, backward:
@requests.reverse_each{|request| puts "#{request}"}

# output
# #<LogRecord:0x007fc44a816210>
# #<LogRecord:0x007fc44a817b10>
# #<LogRecord:0x007fc44a81d3d0>
# #<LogRecord:0x007fc44a81ecd0>
# #<LogRecord:0x007fc44a824630>
# #<LogRecord:0x007fc44a825ee0>
# #<LogRecord:0x007fc44a827790>
# #<LogRecord:0x007fc44a82d0c8>
# #<LogRecord:0x007fc44a82e9a0>
# #<LogRecord:0x007fc44a8343a0>
# #<LogRecord:0x007fc44a836010>
# #<LogRecord:0x007fc44a837910>
# #<LogRecord:0x007fc44a8350e8>
# #<LogRecord:0x007fc44a8367e0>
# #<LogRecord:0x007fc44a837ed8>
# #<LogRecord:0x007fc44a82d640>
# #<LogRecord:0x007fc44a82ed60>
# #<LogRecord:0x007fc44a8244c8>
# #<LogRecord:0x007fc44a825b98>
# #<LogRecord:0x007fc44a827268>
# #<LogRecord:0x007fc44a81c9a8>
# #<LogRecord:0x007fc44a81e0a0>
# #<LogRecord:0x007fc44a81f7c0>
# #<LogRecord:0x007fc44a814f50>
# #<LogRecord:0x007fc44a816648>
# #<LogRecord:0x007fc44a817d40>
# #<LogRecord:0x007fc44a80d480>
# #<LogRecord:0x007fc44a80eb78>
# #<LogRecord:0x007fc44a8042b8>
# #<LogRecord:0x007fc44a8059d8>
# #<LogRecord:0x007fc44a807878>

# all heroku requests in simple format (backward):
@requests.reverse_each(:simple){|request| puts "#{request}"}

# output
# GET /images?q
# GET /images?q
# GET /images?q
# GET /images/spinner.gif
# GET /news?q
# GET /images/spinner.gif
# GET /images/x-gray.png
# GET /web?q
# GET /images/example.png
# POST /login
# GET /favicon.ico
# GET /javascript/jquery.jscroll.min.js
# GET /javascript/main.js
# GET /javascript/jquery-2.1.0.min.js
# GET /stylesheets/main.css
# GET /login?q
# GET /
# GET /
# GET /web?q
# GET /web?q
# GET /images/spinner.gif
# GET /images/x-gray.png
# GET /web?q
# GET /favicon.ico
# GET /javascript/main.js
# GET /images/example.png
# GET /javascript/jquery.jscroll.min.js
# GET /stylesheets/main.css
# GET /javascript/jquery-2.1.0.min.js
# GET /
# GET /

Leave a Reply

Your email address will not be published. Required fields are marked *