To Lecture Notes

IT 232 -- 10/12/15

Review Exercises

  1. What is the value of @c after these controller statements?
    a = [2, 3, 5, 8, 9, 12]
    b = [3, 5, 6, 9, 11]
    @c = a - b
    
    Ans:
    [2, 6, 12]
    
  2. What is the spaceship operator?
    Ans: It is an operator that looks like this: <=>. It performs a three-value comparison for objects. If n > m, n <=> m returns 1; if n < m, n <=> m returns -1; if n == m, n <=> m returns 0. If the <=> operator is defined in the model, it can be used to tell the Ruby sort method how to sort an array of objects.
  3. What are the values of @e and @f after these controller statements?
    n = 3
    m = 8
    @e = "%d %d %d\n" % 
      [n <=> m, m <=> n, m <=> m]
    
    s = "orange"
    t = "apple"
    @f = "%d %d %d\n" % 
      [s <=> t, t <=> s, t <=> t]
      
    Ans:
    -1 1 0
    1 -1 0  
    
  4. Try out this ERB code for pluralizing a noun:
    <p>User has <%= pluralize(@n, "photo") %>.</p>
    
    Set the value of @n in the controller.
  5. What does self mean?
    Ans: self refers to the current object in the model code.
  6. Write a Ruby program that reads the Edgar Allen Poe poem poem.txt and display it in a view.
    Ans: See the Poem Example.

Project 2

Many-to-many Relationships

Sorting an Array of Objects