> rails server --port=3726
<p><% "The Fahrenheit temperature is ', f %></p>
<!-- Version 1: --> <p>The Fahrenheit temperature is <%= @f %></p> <!-- Version 2: --> <p><%= "The Fahrenheit temperature is " + @f.to_s %></p>
x = 5 Ans: assigns the value 5 to the variable x. x.class Ans: returns the name of the class to which x belongs.
print "Hello, World.\n"
grand_total = 256
# Controller code: def display @in = 5 @cm = 2.54 * @in end <!-- View Code --> <p>Number of centimeters is: <%= @cm %></p>
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)
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.
c = 20 f = 9 * c / 5 + 32 puts f
input = STDIN.gets c = input.chomp.to_i f = 9 * c / 5 + 32 puts f
c = 20 f = 9 * c / 5 + 32 puts fto
c = 20 @f = 9 * c / 5 + 32This code is placed into the controller method corresponding to the view that will contain the output. Then put this ERB code on the view page:
<%= @f %>
object : class :: cookie :: cookie-cutter
x = 5 x.class x.methods
r = 17 % 3 print r, "\n"
# Create a new Student object. s = Student.new # Save a Student object in the table. Student.save # Return the first record in the table. Student.first # Return the last record in the table. Student.last # Return number of rows in table. Student.count # Get student with id=5 s = Student.find(5) # Get student with stud_id=4534, using dynamic finder method s = Student.find_by_stud_id(4534) # Get student named Bob s = Student.find_by_first_name("Bob") # Create and save new student s = Student.new s.first_name = "Patricia" s.year = 2 s.gpa = 3.45 s.save