Skip to main content

HW 1: Getting started with Ruby

Warning! This homework assignment should take you about five hours. Make sure that you start with enough time and that you plan breaks to ask questions.

  1. Do the Codecademy Ruby Tutorial through section 10a. (Everything other than do “Banking on Ruby” and the final project.) For those who care about clarity, please note that shift-tab should allow you to re-indent code.

  2. Answer the following questions.

    a. There are at least three techniques for reading a line of text, dropping the newline character, and converting to lowercase.

     ; version 1
     text = gets
     ; version 2
     text = gets.chomp
     ; version 3
     text = gets.chomp.downcase

    Explain the difference. Which does the codecademy course seem to prefer? Which do you think I prefer? Why?

    b. In the exploration of loops, you wrote a short program that replaced each instance of a word with the text REDACTED. Rewrite that program without a loop.

    c. In the histogram example, the introductory code includes the following line.

     frequencies = frequencies.sort_by {|a, b| b }

    Explain what is going on in this example in the context of what you know of hash tables from CSC 207.

    d. You may have observed that Ruby methods permit a variable number of parameters and default values. Explain the syntax for each.

    e. Suppose B is a subclass of A. How do we design B so that calls the A’s initialize method? How do we design B so that does not call A’s initialize method?

  3. Expand the movie database project from lesson 6 (Hashes and Symbols) so that

    a. It provides somewhat better feedback.

    b. It repeatedly prompts the user for choices (and carries out the corresponding action) until the user types "quit".

    c. It does some error checking or input validation (e.g., on the rating).

  4. Exercise 12 of lesson 10 (Object-Oriented Programming II) has you use mixins to provide a jump method for both a Rabbit class and a Cricket class. Extend the example so that objects keep track of and report on the total distance that they’ve jumped.

  5. Email me your answers and code in the body of an email message entitled “CSC 321.01 2018S, HW 1: Getting started with Ruby (Name)”. Please replace “Name” with your name and do not include the quotation marks.