CSC302 2011S Programming Languages

Laboratory: Ruby (2)

Summary:

Prerequisites: The first Ruby lab. Tate 2.3.

Contents:

Preparation

a. Create a directory for this lab. I would recommend something like csc302/ruby/lab2, but it's up to you.

b. Start the interactive ruby interpreter with irb.

c. In your browser, you may want to open a link to the code from Tate and some additional code for this lab.

Exercises

Exercise 1: Fun with Functions

a. Write a function, inc, that takes an number as a parameter and returns the result of adding 1 to that number. (Okay, it's fine if this function takes any parameter that supports + 1.)

b. Try running inc on a variety of types (integers, strings, arrays, whatever).

c. Write a function double, that takes any addable value and returns the result of adding the value to itself.

d. Try running double on a variety of types.

e. Write a function, square, that takes any multipliable value as a parameter and returns that value times itself.

f. Try running square on a variety of types.

Exercise 2: Fun with Classes

Consider the Box class defined below:

class Box
  @@count = 0

  def initialize
    @count = 0
    @@count = @@count + 1
  end

  def inc
    @count = @count + 1
  end

  def to_i
    @count
  end

  def boxes
    @@count
  end
end

a. What do you expect the result of the following to be?

x = Box.new()
x.inc
x.inc
x.to_i

b. Check your answer experimentally.

c. What do you expect the result of the following to be?

y = Box.new()
y.inc
y.to_i
y.boxes
z = Box.new()
y.boxes

Exercise 3: Tree Basics

a. In the section entitled Defining Classes, Tate gives an examples of a simple Tree class. Replicate those examples.

b. Build your own tree for the following hierarchy.

President
  Vice President for Academic Affairs
    Department Chair
      Faculty Member
    Assistant Dean
  Vice President for Student Affairs
    Dean of Housing
    Dean of Academic Advising

c. Write an instruction to print the elements of your tree that contain the word Dean.

Exercise 4: Alternate Tree Constructors

In his problems for this section, Tate suggests that you build a Tree constructor that accepts a nested hash as a parameter. Write that constructor.

Note: While I may not agree with him that a nested hash is more natural than what we've been doing, I do think the problem is a nice way for you to explore hashes, classes, and recursion.

Exercise 5: Mixins and Files

Tate provides a ToFile mixing example.

a. Verify that the example works as advertised for the particular example.

b. Create another Person, also named Matz and see what happens when you write it to a file.

c. Mix ToFile into the Fixnum class and see what happens when you try to write an integer to a file.

d. Create your own simple class and use ToFile to write it to a file.

For Those With Extra Time

Extra 1: Grep

Do Tate's grep exercise at the end of Section 2.3.

Extra 2: Function Composition

You may recall the composition operator from mathematics or CSC 151. In particular, the result of composing two functions, f and g, is a new function that first applies g and then applies f to the result.

Try to write compose in Ruby.

 

History

Saturday, 22 January 2011 [Samuel A. Rebelsky]

  • Set up placeholder for lab.

Friday, 28 January 2011 [Samuel A. Rebelsky]

  • Wrote lab.

Monday, 31 January 2011 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Apr 25 08:06:52 2011.
The source to the document was last modified on Mon Jan 31 08:20:35 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/ruby-2.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/ruby-2.pdf

You may wish to validate this document's HTML ; Valid CSS! ; Creative Commons License

Samuel A. Rebelsky, rebelsky@grinnell.edu