- Name some methods of the Array class.
Ans: new, length, delete, empty?, [ ], ==
- Name some methods of the Range class.
Ans: new, begin, end, to_a, include?, ==
- How to you place a literal double quote character in
a Ruby String object?
Ans: Use \" .
- What is shown in the browser?
<ol>
<li>Line 1<br /></li>
<li>Line 2<br /></li>
<li>Line 3</li>
</ol>
Ans: The output is
- Line 1
- Line 2
- Line 3
If you want the output to be double spaced, use this code instead
<ol>
<li>Line 1<br /> </li>
<li>Line 2<br /> </li>
<li>Line 3</li>
</ol>
- How can you change the type of numbers used in an ordered list?
Ans: Use the following css style:
ol { list-style-type: upper-roman; }
- What does each of these Ruby code fragments do?
# Problem 2a.
(1..3).each do |i|
(1..3).each do |j|
print "*"
end
end
print "\n"
Ans:
*********
# Problem 2b.
(1..3).each do |i|
(1..3).each do |j|
print "*"
print "\n"
end
end
Ans:
*
*
*
*
*
*
*
*
*
# Problem 2c.
(1..3).each do |i|
(1..3).each do |j|
print "*"
end
print "\n"
end
Ans:
***
***
***
- What is the output?
s = ["cat", "dog", "mouse"]
t = ""
s.each do |x|
t = x + t
end
print t, "\n"
Ans:
mousedogcat
- Find the mistakes in this view and controller code:
-------------- input.html.erb View Code --------------------
<% form_tag "receipt" %>
<%= text_field_tag :last_name, "" %>
<%= text-field-tag :first name, "" >
<%= text_field_tag : item, "" >
<%= text_field_tag :price, '' %>
<%= end %>
-------------- purchase_controller.rb Controller Code ------
function purchase
first name = params(:first name)
end
-------------- receipt.html.erb View Code ------------------
<h2>Receipt<h1>
<p>"Thankyou" + <%= first name %> + "for your purchase.</p>
------------------------------------------------------------
Corrected Version:
-------------- input.html.erb View Code --------------------
<%= form_tag "receipt" %>
<%= text_field_tag :last_name, "" %>
<%= text_field_tag :first_name, "" %>
<%= text_field_tag :item, "" %>
<%= text_field_tag :price, '' %>
<% end %>
-------------- purchase_controller.rb Controller Code ------
def purchase
@first_name = params[:first_name]
end
-------------- receipt.html.erb View Code ------------------
<h2>Receipt<h2>
<p>Thankyou <%= @first_name %> for your purchase.</p>
------------------------------------------------------------
- Convert this style to hex color codes:
body { background-color: rgb(217, 58, 13); }
Ans:
body { background-color: #d93a0d; }
- What are the hex color codes for these HTML colors:
yellow
purple
lime
olive
teal
silver
Ans: #ffff00
#800080
#00ff00
#808000
#008080
#c0c0c0
- Use FormHelper methods when you are using a form
to edit a row in a database, or to add a new row to the database.
- Use FormTagHelper methods when the piece of data you are inputing does
not directly correspond to any field in the database.
- References:
api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
- Form
FormTagHelper Version:
<% form_tag 'show' do %>
... Controls go here.
<% end %>
FormHelper Version:
<% form_for @item, :url => { :action => 'show' } do |f| %>
... Controls go here.
<% end %>
or shorter:
<% form_for @item, 'show' do |f| %>
... Controls go here.
<% end %>
- Textfield
FormTagHelper Version:
<%= text_field_tag :celsius, '20', :class => 'tf' %>
:celsius is the name; '20' is the initial value.
FormHelper Version:
<%= f.text_field :celsius, :class => 'tf' %>
:celsius is the field of the database object f.
- Dropdown Menu
FormTagHelper Version:
<%= select_tag :source_rate,
options_for_select([['Dollars', 1.00000],
['Euro', 1.36749],
['Pesos', 12.6149],
['Pounds', 1.58228]]),
:class => 'ddm' %>
:source_rate is the name;
the first column of the array are the values displayed in the
dropdown menu; the second column are the values returned to the
controller to be read by a params statement.
the option tags are the HTML option tags.
FormHelper Version:
<%= f.select :source_rate,
[['Dollars', 1.00000],
['Euro', 1.36749],
['Pesos', 12.6149],
['Pounds', 1.58228]],
:class => 'ddm' %>
:source_rate is the field of the database object f;
the values of the dropdown menu are specified in the array as they were
in the FormTagHelper version; note that for the FormHelper version,
no options_for_select method is needed.
- Radio Button
FormTagHelper Version:
<%= radio_button_tag :group, value, true %>
:group sets the button group (in a group of radio buttons, only can be
checked at a time)
:value is the value returned to the controller
to be read by a params statement
true means that the radio button should be
initially checked.
FormHelper Version:
<%= f.radio_button :group, value, :checked => true %>
:group sets the button group (in a group of radio buttons, only can be
checked at a time)
value is the value returned to the controller
to be read by a params statement
:checked => true means that the radio button should be
initially checked.
- CheckBox
FormTagHelper Version:
<%= check_box_tag :sugar, 1, true, :class => 'cb' %>
:sugar is the name of the checkbox
1 is the value returned to the controller in a params statement
false means that the checkbox is initially checked.
FormHelper Version:
<%= f.check_box :sugar, 1, :checked => true, :class => 'cb' %>
:sugar is the name of the checkbox
1 is the value returned to the controller in a params statement,
0 will be returned if the box is not checked.
:checked => true means that the checkbox is initially checked.
- See the OnlineQuiz and CreditCard (Examples 28 and 29).
Use these as examples to help you for Project 5.