Chapter 11 - Documentation

Exercise 1: Looking up docs for a Ruby core class

Using the Ruby core class Time, write a program to print the current date in the format below.

Today is Thursday, April 14, 2016

We did a Web search for “ruby Time”, and found the following page: Ruby Time class.

Here are the methods we used:

And here’s the resulting code:

DAYS_OF_WEEK = ["Sunday", "Monday", "Tuesday", "Wednesday",
                "Thursday", "Friday", "Saturday"]
MONTHS = {
  1 => "January",
  2 => "February",
  3 => "March",
  4 => "April",
  5 => "May",
  6 => "June",
  7 => "July",
  8 => "August",
  9 => "September",
  10 => "October",
  11 => "November",
  12 => "December"
}

time = Time.now
day = DAYS_OF_WEEK[time.wday]
month = MONTHS[time.month]
puts "Today is #{day}, #{month} #{time.day}, #{time.year}"

Sample output might look like this:

Today is Friday, July 29, 2016