Chapter 7 - Hashes

Exercise 1: Convert months

Below is an english_date method that takes an ISO date (a standardized format with the year, month, and day, in that order) and converts it to plain English, with the name of the month spelled out. So, “2007-02-24” would be converted to “February 24, 2007”.

The month_names variable needs to be set to a hash with the month numbers as keys, and the month names as values.

def english_date(iso_date)
  month_names = {
    "01" => "January",
    "02" => "Feburary",
    "03" => "March",
    "04" => "April",
    "05" => "May",
    "06" => "June",
    "07" => "July",
    "08" => "August",
    "09" => "September",
    "10" => "October",
    "11" => "November",
    "12" => "December"
  }

  date_parts = iso_date.split("-")
  year = date_parts[0]
  month = date_parts[1]
  day = date_parts[2]
  "#{month_names[month]} #{day}, #{year}"
end

# Should print "June 20, 2002"
puts english_date("2002-06-20")
# Should print "December 13, 1974"
puts english_date("1974-12-13")