To Lecture Notes

IT 231 -- 7/30/12

 

Review Questions

  1. Which of these are legal Ruby variable names?.
  2. List some Ruby operators. Ans:
  3. How can you convert an integer into a string?
    Ans:   n.to_s
     
  4. How can you convert a string into an integer?
    Ans:   s.to_i
     
  5. What is the difference between print and puts in Ruby?
    Ans: print allows you to print multiple items at a time like this:
    puts prints a single string like this:
    puts automatically adds "\n" at the end of the string, so it's not necessary to explicitly include it.
     
  6. Explain the difference between Ruby classes and objects.
    Ans: A Ruby class forms the blueprint or design for an object. If A is the class name, then x = A.new creates a new object. You can create as many objects as you wish from one class.
     
  7. Explain what the Ruby dot operator does.
    Ans: It calls a method. Methods can either be class methods (called from a class like t = Time.new or object methods (called from an object like t.hour).
     
  8. What will this Ruby line print?
  9. Ans: It will print 8 if August is the current month.
     
  10. Try out these Ruby statements in IRB. What do you think is happening?
  11. Ans: This is an example of variable interpolation. #{a} means insert the value of the variable a at this location in the string.
  12. Change the double quotes in Question 10 to single quotes. Explain.
    Ans: Variable interpolation and escape characters only work with double quotes, not single quotes.
     
  13. Write a Ruby line that prints a random number from 0 to 10 with equal probability.

     
  14. Write a Ruby line that assigns to employee_id, a random number 1001 to 1999 with equal probability.

     
  15. What is the value of a after these Ruby statements?
Ans: no (Because the condition x + 2 > 9 is false, perform the action of the else clause, which is a = "no".)

 

Control Structures

 

Modifying Ruby Code to Accept User Input

 

Modifying Images with Image Editing Software

 

Project 4

 

The Array Class

 

Ruby each Statement

 

Practice Problems

  1. Create a Rails model GradeRecord with fields stud_id: integer, last_name:string, first_name:string, course_number:string, course_name:string, prof_name:string, credit_hours:integer, grade:string. Append this Ruby seed code to the file db/seeds.db. You can then seed the database with startup values by typing
    Now start the Rails console and load all of the records from the database into an array with Use for..each statements or each methods to print the first and last names for all the grade records:
  2. Create a Rails model that contains Student objects with fields name (string), year (integer), gpa (float), fulltime (boolean). Then use the Rails console to:
     
    1. add three new Person objects to the database.
       
    2. shut down the server and start the Rails console.
       
    3. load all of the records from the database into an array with
        a = Person.all
        
    4. Use for..each statements or each methods to print the names of all the students to the console.
     
  3. Write a Ruby script that inputs the course score and outputs the course grade.
    A == 90..100, B == 80..89, C == 70..79, D == 60..69, F == 0..59. Use an if..elsif..end statement.

 

Review for Midterm