From: Aqua Fina Date: 2006-08-10T05:57:16+09:00 Subject: Re: Simple looping question Max Muermann wrote: > equire 'yaml' > require 'enumerator' > > data = YAML::load File.read('data.yml') > data.entries.sort_by{|date, *| date }.each_cons(2) do | first, second | > date_first = DateTime.parse(second[0].to_s) > date_second = DateTime.parse(first[0].to_s) > duration = date_first - date_second > miles = second[1][1] - first[1][1] > miles_per_gallon = miles / first[1][0] > p "The #{first[1][0]} gallons of gas purchased on > #{date_first.strftime('%m/%d/%Y')} lasted #{duration.to_i} days. The > mileage was #{miles_per_gallon} miles per gallon." > end > > The key here is the each_cons coming from the enumerable mixin, which > iterates the array in pairs of twos. Brilliant. That enumerator mixin is crazy awesome. Thanks. > Disclaimer: I am a German living in Australia, so if I got the date > format or the miles per gallon calculation wrong, blame the non-metric > system. > > Max -- Posted via http://www.ruby-forum.com/.