CSC302 2011S Programming Languages

Laboratory: Ruby (1)

Summary: We begin our exploration of Ruby with a few simple and straightforward problems.

Prerequisites: Ability to run programs on our Linux system. Sections 2.1 and 2.2 of Tate.

Preparation

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

b. Start the interactive ruby interpreter with irb.

Exercises

Exercise 1: Fun with Output

a. Type in the example from Using Ruby from the Console on page 12 of Tate and verify that you get the same output that Tate indicated.

b. What do you expect the output of the following instruction to be?

puts 'hello, #{language}'

c. Check your answer experimentally.

d. What do you expect the output of the following instruction to be?

puts "Hello, #{person}"

e. Check your answer experimentally.

f. What do you expect the output of the following instructions to be?

x = 5
y = 7
puts "#{x} + #{y} = #{x+y}"

g. Check your answer experimentally.

h. Ruby provides both a print operation and a puts operation. Experimentally determine some differences between the two.

Exercise 2: Fun with Strings

Much of this exercise is taken from Tate 2.2.

a. Write an instruction to print the string Hello, World.

b. Write an instruction to determine the index of the word World in Hello, World.

c. Write an instruction to replace the word World with Ruby in Hello, World.

Exercise 3: Iteration

Based on problems from Tate 2.2.

a. Verify that the looping instructions Tate provides on page 15 (the ones that follow the fragment while and until are similar work as advertised.

b. Write an instruction to print your name ten times.

c. Write an instruction to print This is sentence number i, where the number i ranges from 0 to 9.

d. Write instructions to print a multiplication table for the numbers 0 through 9. (Bonus: Format it nicely.)

Exercise 4: Fun with Files

a. Put your instructions for printing a multiplication table in a file.

b. Figure out how to load that file into the the Ruby interpreter.

c. Figure out how to make an executable that includes a ruby program.

Exercise 5: Blocks

In Scheme, functions are first class objects. That means that you can assign functions to variables, pass them as parameters, and return them from functions.

Ruby's blocks are much like anonymous functions. Determine whether or not Ruby makes blocks first-class objects.

Be prepared to discuss what experiments you tried and why you think Matz made the decision he did.

Exercise 6: Scoping

Consider the following function

def add_four_to_x
  4 + x
end

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

x = 5
add_four_to_x

b. Check your answer experimentally.

c. Suppose we defined x before defining add_four_to_x. What do you expect the results to be?

d. Check your answer experimentally.

Exercise 7: Scoping, Revisited

Consider the following code:

x = [1,2,3]
x.each {|x| puts x}

a. What do you expect the output to be?

b. Check your answer experimentally.

c. Is there a way to access the outer x?

For Those With Extra Time

Extra 1: Guessing Games

Based on a problem in Tate 2.2.

a. Write a program that picks a random integer in the range 0 to 100 and lets the user try to guess the number. You should tell the user whether the guess is too low or too high.

b. Write a program that lets the user pick an integer and tries to guess that integer.

 

History

Saturday, 22 January 2011 [Samuel A. Rebelsky]

  • Set up placeholder for lab.

Wednesday, 26 January 2011 [Samuel A. Rebelsky]

  • Filled in the details.

 

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:51 2011.
The source to the document was last modified on Wed Jan 26 09:26:23 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/ruby-1.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/ruby-1.html

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

Samuel A. Rebelsky, rebelsky@grinnell.edu