CS 3721
Programming Languages  
Fall 2013
Recitation 11. 
 Ruby: Reg. Expr.
Week 12: Nov 11 - 15

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


Ruby and Regular Expressions: Ruby is a fully object-oriented language. Even an integer constant is an object with methods that can be applied to it.

In particular, Ruby regular expressions are implemented in an object-oriented fashion. After implementing them, the inventor of Ruby (Matz) added extra $ variable so that they could be used much like those in Perl if one wanted to.

In this recitation, I require that you study and use the object-oriented versions that are basic to Ruby and not found in Perl at all.

You should also read and go over my Ruby pages:


Ruby programs to write and debug, each involving 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:

    Input Data to Process
    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:

    Proper Output Data
    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 (random)
      8:04 am January 4, 19981998-01-04 08:04:00 (random)
    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. As above, do not check for input errors.)

      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.

  • 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.

  • 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.

  • But don't get too compulsive about the details. You can make reasonable assumptions without asking me if it's all right.


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