#!/usr/bin/env ruby -w # # Birthday Helper # "Learn to Program" by Chris Pine, p. 114 # require 'time' all_birth_days = {} all_birth_years = {} file = File.read('birthdays.txt').split("\n") file.each do |line| name, day, year = line.split(',').collect { |s| s.strip } all_birth_days[name] = day all_birth_years[name] = year end print "Who? (or 'quit') " s = gets.chomp while s.downcase != 'quit' birth_day = all_birth_days[s] birth_year = all_birth_years[s] if birth_day.nil? puts "I don't seem to have a birthday recorded for #{s}" else birth_day_t = Time.parse(birth_day) current_year = Time.new.year current_birth_day = Time.mktime(current_year, birth_day_t.month, birth_day_t.day) if Time.new > birth_day_t # passed it birth_day_t = birth_day_t + (60 * 60 * 24 * 365.25) end age = current_year - birth_year.to_i puts "#{s}'s #{age}th birthday is #{birth_day_t}" end print "Who (or 'quit')? " s = gets.chomp end