To Lecture Notes

IT 231 -- 8/6/12

 

Review Questions

  1. What is the value of this Ruby expression?
  2. What is the output:
    Ans: Note than when an assignment is made, no output occurs until a print statement is executed.
    In line 1, a is assigned the value of "apple".
    Since the length of a is 5 characters, lines 3 and 4 are not executed because only the else statements (lines 6 and 7) are executed.
    This causes b to be assigned the value of "apricot" and the first output "orange" to be printed. Because of the \n, orange is on a line by itself.
    Now the final print statement in line 9 occurs: The literal values are printed, except when #{a} or #{b} occurs.
    #{a} and #{b} mean insert the values a and b into the expression. The output from lines 6 and 7 is
    The complete output is:
  3. What is the output?
  4. Create a Rails view that displays randomly one of the images 1.png, 2.png, 3.png, or 4.png. Ans:
  5. What does the String method chomp do?
    Ans: It removes a trailing \n character from a string.
     
  6. Modify Example 21 (LongSong Example) so that it accepts input from the keyboard.
    Ans: Replace the line with
  7. Rewrite this Ruby source code to display the output in a view.
    Ans: Here are the controller and view code:
  8. Create the Ruby arrays letters1 and letters2 containing the letters "a", "b", "c", "d" using the short method and the long method, respectively. Ans:
  9. Give some examples of one-to-many relationships. Ans:
     
    Primary
    Model
    Secondary
    Model
    Foreign
    Key
    * Movie Review movie_id
    * Student Owner student_id
    * Pet Pet owner_id
    AirlineFlight Passenger airline_flight_id
    Store StockItem store_id
    SportsTeam Player sports_team_id

    * = do not use for your Project 5 because it was a class example.
     
  10. What is a foreign key?
    Ans: The field that links a record in the secondary model to its owner in the primary model. Rails foreign keys always have the suffix "_id".
     
  11. List the steps to setting up a one-to-many relationship.
    Ans: See the One-to-many Relationships Tutorial.
     
  12. What does a one-to-many relationship do for you in a Rails scaffold project?
    Ans: If Student is the primary model, Award is the seconary model, s is a Student object, and a is an Award object,
    1. s.awards gives the array of all Award objects that belong to s;
    2. a.student gives the Student object that owns a.

 

The Array Method collect

 

One-to-many Relationships

 

The Range Class

 

Sending Data from a View Back to the Controller

 

Catching Data Sent from a View by the Controller

 

Hex Color Codes

 

The DisplayGradeData Example