CSC302 2011S Programming Languages
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
Summary:
Prerequisites: The first Ruby lab. Tate 2.3.
Contents:
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.
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.
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
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
.
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.
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.
Do Tate's grep exercise at the end of Section 2.3.
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.
Saturday, 22 January 2011 [Samuel A. Rebelsky]
Friday, 28 January 2011 [Samuel A. Rebelsky]
Monday, 31 January 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/ruby-2.html.
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
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
;
;