# Simple looping question

**URL:** <https://www.ruby-forum.com/t/simple-looping-question/101947>\
**Category:** Ruby\
**Created:** [August 8, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/simple-looping-question/101947 "2006-08-08T20:49:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aquafina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@aquafina](https://www.ruby-forum.com/u/aquafina)\
**Post date:** [August 8, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/simple-looping-question/101947/1 "2006-08-08T20:49:34Z")

</div>

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.

Thanks.

---

<div class="post-metadata">

**Author:** ![aquafina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@aquafina](https://www.ruby-forum.com/u/aquafina)\
**Post date:** [August 8, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/simple-looping-question/101947/2 "2006-08-08T21:03:29Z")

</div>

How about looping through the list backwards?

---

<div class="post-metadata">

**Author:** ![aquafina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@aquafina](https://www.ruby-forum.com/u/aquafina)\
**Post date:** [August 8, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/simple-looping-question/101947/3 "2006-08-08T21:27:31Z")

</div>

Aqua Fina wrote:

> 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

---

<div class="post-metadata">

**Author:** ![aquafina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@aquafina](https://www.ruby-forum.com/u/aquafina)\
**Post date:** [August 9, 2006, 4:18am UTC](https://www.ruby-forum.com/t/simple-looping-question/101947/4 "2006-08-09T04:18:52Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![aquafina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@aquafina](https://www.ruby-forum.com/u/aquafina)\
**Post date:** [August 9, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/simple-looping-question/101947/5 "2006-08-09T22:57:12Z")

</div>

Max M. 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
