CS169 Fall 2010 Midterm Review

Major Topics

(Non-Comprehensive) Review Questions

1.        (Jim Weirich) Write an iterator fib(n) that iterates over the first n Fibonacci numbers.  Example usage:

>> fib(5) { |x| puts x }






2.        How do you define a class method?  How do you define an instance method?  How does using one differ from the other?

3.        (Karl) Bill Ogre wants to set up his own blogging application, but doesn’t have a clue how to do it. Help him out by describing each CRUD operation to him in terms of a blog, as well whether it is a PUT, POST, GET, or DELETE action.

(a) index:

(b) create:

(c) read:

(d) update:

(e) destroy:

4.        For (a) and (b) above, specify which actions should occur in the Model, which should occur in the Controller, and which occur in the View if the implementation follows the MVC model.



5.        Part of a large application you’re working on includes some simple blogging functionality.  Working with the client, you come up with a Feature that looks like:

Feature: In order to keep customers updated on product news

As a site administrator

I want to be able to add news entries to the front page

Write a Scenario for this feature (any reasonable Scenario is fine).  Be sure to follow the correct format.

6.        When working on implementing a Scenario, when is a good time to start using RSpec for unit testing?

7.        Jane the Java Programmer wrote an implementation of every_nth, which is used to apply a function to every nth item of a collection.  

Example usage:

def times2(x)

  x = x*2


>> every_nth(2, [1 2 3 4], :times2)

=> [2, 6]

Which SOFA/Ruby/good code principles are being violated in this function? Refactor it.

def every_nth(n, coll, name_of_func_to_apply)

  if coll.class == Array

    ret = []


    raise Exception.new("every_nth only works with Array, \\

you passed #{coll.class}")


  curr = 0

  for x in (1..coll.size)

    if x % n == 0

      ret[curr] = send(name_of_func_to_apply, coll[x])



  return ret


8.        You are working on a web application that allows users to purchase a t-shirt which changes each day.  The user clicks on the shirt, which brings up a “Order Preview” page that they must finalize/accept before the order is actually put into the database; this order preview uses the user’s cookie to figure out her billing information.  Describe how to implement the following actions RESTfully, including reasonable URLs.

Order preview:



  Action on database:




  Action on database: