- How do you change the port number to 3726 for the WEBrick server?
> rails server --port=3726
- What is the range of numbers for well-known ports?
Ans: 1 to 1023.
- What does \n mean in Ruby?
Ans: It is the new line character.
- Find the mistakes in this line of an .html.erb file:
<p><% "The Fahrenheit temperature is ', f %></p>
Ans: Here are two possible corrected versions:
<!-- Version 1: -->
<p>The Fahrenheit temperature is <%= @f %></p>
<!-- Version 2: -->
<p><%= "The Fahrenheit temperature is " + @f.to_s %></p>
- What do these Ruby statements do?
x = 5 Ans: assigns the value 5 to the variable x.
x.class Ans: returns the name of the class to which x belongs.
- Write a Ruby script that prints "Hello, world."
print "Hello, World.\n"
- Write a Ruby statement that assigns 256 to the grand total.
grand_total = 256
- Write controller code with a view that converts 5 inches to centimeters
and displays the answer. (1 inch = 2.54 cm). Ans:
# Controller code:
def display
@in = 5
@cm = 2.54 * @in
end
<!-- View Code -->
<p>Number of centimeters is: <%= @cm %></p>
- List six ways of running Ruby code. Ans:
- Interactive Ruby (IRB)
- Ruby Script
- Embedded Ruby (ERB) in a view
- In the Rails controller
- From the Rails Console
- Load from the Rails Console
- From a Rails seed file.
- Which of these are legal Ruby variable names?.
abc (legal)
b768 (legal)
4slots (illegal, starts with a digit)
First Name (illegal, can't have embedded space)
_n_ (legal)
hook&ladder (illegal, can't use &)
numberOfCustomers (legal, but not recommended Ruby syntax. Use underscore notation.)
number-of-customers (illegal, can't use dash in variable name)
PreferredCustomer (legal as a class name, which uses upper camel casing)
an_extremely_long_variable_name_if_you_ask_me (legal, but should be shorter)
- Try out these Ruby statements in IRB. What do you
think is happening?
a = "Jeremy"
b = 342
print "My friend #{a} owes me $#{b}.\n"
Ans: This is an example of variable interpolation. #{a} means insert
the value of the variable a at this location in the string.
- Change the double quotes in Question 11 to single quotes.
Explain.
Ans: Variable interpolation and escape characters only work with
double quotes, not single quotes.