To Lecture Notes

IT 231 -- 9/23/15

Review Questions

  1. What is a Ruby Range object? How do you define one?
    Ans: A range object contains all the integers between the start point and end point. For example:
    r = 3..6
    
    or
    r = Range.new(3, 6)
    
    defines a Range object r that contains the values [3, 4, 5, 6].  This type of range is called an inclusive range.
    To define an exclusive range, use 3 dots.
    r = 4..8
    
    or
    s = Range.new(4, 8, true)  # (true means exclusive)
    
    defines an object that includes the values [4, 5, 6, 7].
  2. What is a Ruby Symbol object? How do you define one?
    Ans: A symbol is similar to a String object. Define a symbol like this with this with a : prefix
    s = :dog
    
  3. What is the difference between symbols and strings?
    Ans: The difference is that a String object is mutable (its characters can be changed), whereas a Symbol object is immutable (its letters cannot be changed).
  4. What do the Ruby tokens %q %Q %w %W %x %r mean?
    Ans:
    1. %q denotes a string where you get to pick the delimiter. For example:
      s = %q/This is a "test"/
      
      / is the delimiter, which is useful because the string contains embedded double quotes, so double quotes are not convenient as delimiters.
    2. Use %Q to define a string that contains escape characters and/or expression interpolation like this:
      val = 357
      x = %Q(This\tis\ta\"test". Value=#{val}\n)
      print x
      
      ( and ) are the delimiters. The output is
      This    is    a    "test". Value=357
      
  5. What is a primary key?
    Ans: It a column that uniquely determines a row in the table. This means that all of the primary key values must be different from each other.
  6. What does it mean for a database table to be normalized (3NF)?
    Ans: A database table is normalized if each column in the table only depends on the primary key and not on any other column.
  7. Which of these tables are normalized? Decompose those that are not normalized.
  8. What is a Rails validation and how do you set one up?
    Ans: A validation is set up in a model file. A validation is used to check a field value to be inserted into a database table. If the value passes the validation, the value is inserted into the database. if the value does not pass the validation, an error message is shown and the user is allowed to reenter the value until it passes the validation. See the Validation section below for more details about validations and a link to some sample validations.
  9. What is a one-to-many relationship and how do you set one up in Rails?
    Ans: a one-to-many relationship means that the each row in the secondary table belongs to a row in the primary table. Each row in the primary table has many rows (0 or more) in the secondary table. Here are some examples:
    Primary Table Secondary Table
    advisors students
    airline_flights passengers
    doctors patients
    auto_models parts
    instagram_users photos
    mothers children
    cookbooks receipes
  10. Create the Hospital1 Example and run the seeds.rb file.  Then make these modifications to the project, which are requested in the Hospital Directions file.

Validations