Skip to content

Instantly share code, notes, and snippets.

@burtlo
Last active December 23, 2015 15:39
Show Gist options
  • Save burtlo/6657015 to your computer and use it in GitHub Desktop.
Save burtlo/6657015 to your computer and use it in GitHub Desktop.
[:a,:b,:c].reverse_each.with_index do |item,index|
puts "#{item} #{index}"
end
# c 0
# b 1
# a 2
module Enumerable
def each_with_reverse_index
each_with_index do |item,index|
countdown_index = length - 1 - index
yield item, countdown_index
end
end
end
[:a,:b,:c].each_with_reverse_index do |item,index|
puts "#{item} #{index}"
end
# a 2
# b 1
# c 0
@burtlo
Copy link
Author

burtlo commented Sep 22, 2013

I want output like the following:

# a 2
# b 1
# c 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment