- 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.
- 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.
- Give some possible instance variables and methods for these objects:
VendingMachine HtmlSubmitButton
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
- Name some methods of each of these classes:
Write Ruby code to test them.
Here is the answer.
- 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.
- You have created a controller with three views with
rails g controller ShowInfo home page1 page2
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
- You create a scaffold with
rails g scaffold Pet name:string animal_type:string
birth_date:date owner_id:integer
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.
- 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
- 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.
- 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.
- How can you add a field to an existing model?
Ans: To add the field extension to the Pet model:
rails g migration add_extension_to_pets extension:string
Don't forget to run rake db:migrate to update the database.
- 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.