This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Counting the Days Since an Arbitrary Date
CodeExpectedActual
def last_modified(file)
  t1 = File.stat(file).ctime
  t2 = Time.now
  elapsed = (t2-t1)/(60*60*24)
  puts "#{file} was last modified #{elapsed} days ago."
end
last_modified("/etc/passwd")
/etc/passwd was last modified 125.873605469919 days ago.
/etc/passwd was last modified 37.6953376619329 days ago.
last_modified("/home/leonardr/")
/home/leonardr/ was last modified 0.113293513796296 days ago.
/home/leonardr/ was last modified 0.0442960046180556 days ago.
require 'date'
def advent_calendar(date=DateTime.now)
  christmas = DateTime.new(date.year, 12, 25)
  christmas = DateTime.new(date.year+1, 12, 25) if date > christmas
  difference = (christmas-date).to_i
  if difference == 0
    puts "Today is Christmas."
  else
    puts "Only #{difference} day#{"s" unless difference==1} until Christmas."
  end
end
advent_calendar(DateTime.new(2006, 12, 24))
Only 1 day until Christmas.
Only 1 day until Christmas.
advent_calendar(DateTime.new(2006, 12, 25))
Today is Christmas.
Today is Christmas.
advent_calendar(DateTime.new(2006, 12, 26))
Only 364 days until Christmas.
Only 364 days until Christmas.
sent = DateTime.new(2006, 10, 4, 3, 15)
received = DateTime.new(2006, 10, 5, 16, 33)
elapsed = (received-sent) * 24
puts "You responded to my email #{elapsed.to_f} hours after I sent it."
You responded to my email 37.3 hours after I sent it.
You responded to my email 37.3 hours after I sent it.
require 'date'
def remaining(date, event)
  intervals = [["day", 1], ["hour", 24], ["minute", 60], ["second", 60]]
  elapsed = DateTime.now - date
  tense = elapsed > 0 ? "since" : "until"
  interval = 1.0
  parts = intervals.collect do |name, new_interval|
    interval /= new_interval
    number, elapsed = elapsed.abs.divmod(interval)
  "#{number.to_i} #{name}#{"s" unless number == 1}"
  end
  puts "#{parts.join(", ")} #{tense} #{event}."
end
remaining(DateTime.new(2006, 4, 15, 0, 0, 0, DateTime.now.offset),
          "the book deadline")
27 days, 4 hours, 16 minutes, 9 seconds until the book deadline.
0 days, 6 hours, 27 minutes, 39 seconds until the book deadline.
remaining(DateTime.new(1999, 4, 23, 8, 0, 0, DateTime.now.offset),
          "the Math 114A final")
2521 days, 11 hours, 43 minutes, 50 seconds since the Math 114A final.
2548 days, 9 hours, 32 minutes, 20 seconds since the Math 114A final.