Back to Index

Review Questions

  1. List six ways of running Ruby code.
     
    Ans: IRB, Ruby script file (ruby filename.rb), controller of a Rails project, ERB in a view or layout, rails console, load Ruby script from the Rails console.
     
  2. What is the difference between a class and an object? Explain what instance variables and methods are.
     
    Ans: A class is a blueprint from which multiple objects can be created. Objects are said to be instantiated from a class. An instance variable can be used anywhere in a class, whereas a local variable can only be used in the method where is it defined. A method is a procedure or function. An object method f is called by an object x like this: f.x. A class method can only be called by a class, for example Time.now.
     
  3. Give some possible instance variables and methods for these objects:
     
     
    Ans:
    Vending Machine Instance Variables: @amount_for_purchase, array of @number_of_items_remaining, array of @price
    Vending Machine Methods: accept_coin, dispense_item
    HtmlSubmitButton Instance Variables: @caption, @width, @height, @font, @background_color, @foreground_color
    HtmlSubmitButton Methods: on_click, move, set_focus
     
  4. Name some methods of each of these classes:
     

  5. Write Ruby code to test them. Here is the answer.
     
  6. Implement a ruby Person class with instance variables @name and @age. Implement accessors for @name and @age. Also implement a mutator for @age, a have_birthday method that adds one to @age, and a to_s method that returns the object data as a string.
     
    Here is the answer.
     
  7. You have created a controller with three views with
    You realize that you need to add an additional view named page3. What are the required steps needed to add page3 to the project.
     
    Ans: (i) Add a view named page3.html.erb to app/views/show_info, (ii) add a method named page3 to the controller in app/controllers/show_info_controller.rb, (iii) add the route /show_info/page3 to config/routes.rb
     
  8. You create a scaffold with
    What other fields are automatically added to the database table besides besides the ones explicitly listed in the generate scaffold statement?
     
    Ans: id, created_at, updated_at.
     
  9. Change the dropdown menu for birth_date so that it shows all years from thirty years ago to the present.
     
    Ans: Add these arguments to the f.date_select tag: :start_year => Time.now.year - 30, :end_year => Time.now.year
     
  10. What types of validations do you know about?
     
    Ans: Here are some possible arguments for the validates method: :presence => true, :uniqueness => true, :length => { :within => 5..20 }, :format => /regular expression/, :numericality.
     
  11. What is a fat model?
     
    Ans: A model that contains additional code, defining validations or virtual methods, in addition to the columns in the database table that corresponds to the model.
     
  12. How can you add a field to an existing model?
     
    Ans: To add the field extension to the Pet model:
    Don't forget to run rake db:migrate to update the database.
  13. Verify the named routes and sample URLs in the first table of Scaffold Generated RESTful Routes.
     
    Ans: Look in the controller and views to verify the URLs. You can also issue rake routes from the command prompt.