From: Daniel Schierbeck Date: 2006-08-09T04:25:09+09:00 Subject: Re: Simple looping question Aqua Fina wrote: > I have some data in a YAML file that looks like: > > 20060712: [9.174, 131452] > 20060719: [9.466, 131805] > 20060727: [9.197, 132150] > 20060807: [9.778, 132540] > > It represents dates, gallons of gas, and mileage, respectively. > > I want to loop through and print out something like: > > The W gallons of gas purchased on X lasted Y days. The mileage was Z > miles per gallon. > > What is the best way to do this? I need the next date from the next > entry in the hash, so a simple .each won't work. I can't quite wrap my > head around this problem. You'll need to figure out how to compare two dates, but I'm sure one of the core classes has a way of accomplishing that. As for knowing which date is next: entries = hash.entries.sort_by{|date, *| date } entries.each_with_index do |entry, i| if i < entries.length days = do_comparison(entry[0], entries[i + 1]) ... end end Cheers, Daniel