I have an array @dates, that are UTC dates, and in increasing order. I want to flip the indices of the array so that the dates are in descending order. I am familiar with JS an Java, and don't know how to either use a pointer/index counter in ruby.
@dates = [//dates are in here already]
@reverseDates = []
@dates.each do |d|
@reverseDates << @dates.last
end
@dates = @reverseDates
Part of the issue as well is that I think it is duplicating the last index of @dates, not moving it to the other array when it pushes.
So I got it it working by prepending the array, but how do you include index counters in Ruby to accomplish this?
@reverseDates = []
@dates.each do |d|
@reverseDates.unshift(d)
end
@dates = @reverseDates
@dates.lastnever changes in theeachbecause@datesnever changes in theeach. See Enumerable#each_with_index for how to iterate with an index; however, this is not the real problem, nor the real solution.Enumerablemodule from A to Z, each one of these methods is so powerful!