CS 3721
Programming Languages  
Spring 2013
Recitation 10. 
 Ruby: Reg. Expr.
Weeks 11-12: Apr 1-12

Submit following directions at: submissions and rules at: rules. Deadlines are:
  • 2013-04-12  23:59:59 (that's Fri, 12 Apr 2013, 11:59:59 pm) for full credit.
  • 2013-04-15  23:59:59 (that's Mon, 15 Apr 2013, 11:59:59 pm) for 75% credit.


Reading: For this recitation you should study three chapters of the online book Programming Ruby: The Pragmatic Programmer's Guide. Chapters to study:

  • Ruby.new. Early overview chapter, Chapter 2 in the actual book (14 pages). (Study all of this except for "Hashes" and details of "Blocks and Iterators".)

  • Classes, Objects, and Variables. The next chapter, Chapter 3 in the actual book (17 pages). (Skip the final third of this chapter, starting with "Singletons and Other Constructors" and then skip "Access Control" and "Variables".)

  • Standard Types. Chapter 5 in the actual book (19 pages), the first half is easy and the second half deals with regular expressions. (Skip "Ranges" in the first half, and focus on the final subsection "Object-Oriented Regular Expressions" in the second half, without worrying about other details of regular expressions in the second half.)

You should also read and go over the examples in:


Ruby programs to write and debug (each about regular expressions):

  1. First program using Ruby regular expressions: You must use the object-oriented verion of Ruby's REs and not a version like Perl that uses "$" variables. (See Ruby Regular Expressions

    Suppose you have input data giving student names and student numbers in the following form:

      17 Dennis A. Andrade  @00002222
      18 Isabel Carrera     @00555555
      19 Carlton W. Creech  @00444444
      20 Philip R. Hayden   @00044444
      21 Jason R. Luna      @00077777
      22 Manuel Neri        @00222222
      
    We want to rewrite these in a different way. This assumes that the last name is always followed by one or more blanks and then an '@' character. There may or may not be a middle initial. You should drop the initial number, write the student number without leading 0's, and write the last name first, followed by a comma, followed by a blank and the rest of the name, as shown below:

      2222 Andrade, Dennis A.
      555555 Carrera, Isabel
      444444 Creech, Carlton W.
      44444 Hayden, Philip R.
      77777 Luna, Jason R.
      222222 Neri, Manuel
      

    Write a Ruby program using object-oriented regular expressions to do this translation. [The comments above point to two minor problems that come up with this program: first to eliminate the leading zeros, and second the names with no middle initial. Be sure to submit even if you can't deal with these problems. You may ask me for a hint about these if you want.]


  2. Second Program involving regular expressions: Here are two forms for representing the time and date:

    "American" style International styleComments
    10:03 pm April 20, 20042004-04-20 22:03:00
      8:04 am January 4, 19981998-01-04 08:04:00
    12:00 pm July 4, 20122012-07-04 12:00:00 This is noon
    11:59 pm December 31, 20032003-12-31 23:59:00 1 min. < next entry
    12:00 am January 1, 20042004-01-01 00:00:00 This is midnight

    1. Write a Ruby program that translates International style dates to "American" style dates:
      1. Read a sequence of International style dates, one to a line, and translate these into the American style.
      2. Use Ruby regular expressions as an essential part of the program. (Follow the model of the regular expression programs on the page Ruby Regular Expressions.) You must not use the Perl-like variables $1, $2, etc., but must program this using Ruby's object-oriented features.
      3. To help with the translation, use a Ruby array with the names of the twelve months in it.
      4. Notice that in the first sample date above, the hour of "22" must be changed to a "10" with "pm" at the end. Notice also that in the second sample date above, the "08" for hours and the "04" for days must be written in American style without the leading "0".
      5. Notice that the "American"style leaves off the seconds. You can leave them off also, or put them in if you wish.
      6. Do not check for input errors.

    2. Write a similar program that will translate from American style to International style. (This is similar code, but perhaps a little harder. Assume some reasonable definition for the "American" style, as shown above. Since seconds are left off in the "Ameican" style, you should put :00 back into the International style.)

      In addition to methods to_i (to int) and to_s (to string), You might find the following functions helpful:

      Functions pad0 and strip0
      #!/usr/local/bin/ruby
            
      def pad0(t)
      # if t as an integer is < 10, add 0 at left
         if t.to_i < 10
            "0" + t
         else
            t
         end
      end
      
      def strip0(t)
      # remove 0 at left of input string t (if present)
         if t =~ /^0/
            t[1,t.length-1]
         else
            t
         end
      end
      
      print pad0("9"), "\n"     # prints 09
      print pad0("19"), "\n"    # prints 19
      
      print strip0("03"), "\n"  # prints 3
      print strip0("23"), "\n"  # prints 23
      


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. If two people are working together, be sure that both signed in as a pair and that both names appear at the top of the submission.

 Contents of submission for Recitation 14:

Last Name, First Name; Course Number; Recitation Number.

1. This should be similar to the example in the regular expression page. You must give the program source and results of a run with the data given.

2. For full credit, you might have two Ruby programs that will translate in each direction. You should test each program at least with the five examples shown in the write-up above. However, you can also put everything into one program, which is what I did, producing two functions, one for each of the translations. You should get all the little details of the data correct.


Revision date: 2013-03-23. (Please use ISO 8601, the International Standard Date and Time Notation.)