27 % 8 Ans: 3, because the remainder when you divide 27 by 8 is 3.
a = "apple"
if a.length < 4
b = "pear"
print "grapefruit\n"
else
b = "apricot"
print "orange\n"
end
print "a #{a} b #{b.reverse}\n"
Ans: Note than when an assignment is made, no output occurs until a
print statement is executed."a" + value of a + "b" + value of b reversed, which is "a " + "apple" + " b " + "tocirpa" + "\n"The complete output is:
orange a apple b tocirpa
n = 45 m = 31 print m >= n, "\n" s = "dog" t = "cat" print s >= t, "\n" a = Time.now b = Time.new(2012, 1, 1, 0, 0, 0) print a >= b, "\n" Ans: false (It is false that 31 >= n.) true (It is true that "dog" comes after "cat" in alphabetic order.) true (It is true that the time a comes after the time b.)
# Long version of controller code:
number = 1 + rand 4
if number == 1
img = "1.jpg"
elsif number == 2
img = "2.jpg"
elsif number == 3
img = "3.jpg"
else
img = "4.jpg"
end
# Short version of controller code:
number = 1 + rand(4)
img = "#{number}.jpg"
# Even shorter version:
img = "#{1 + rand(4)}.jpg"
# View code:
<%= image_tag img, :class = "imgclass" %>
# CSS code for image class:
.imgclass { width: 200px; }
n = 10 + rand(90)with
print "How many verses do you want?" n = STDIN.gets.chomp.to_i
#--- app/controllers/prediction_controller.rb
class PredictionController < ApplicationController
def show
number = rand(6)
if number == 0
response = 'As I see it yes'
elsif number == 1
response = 'Most likely'
elsif number == 2
response = 'Reply hazy, try again'
elsif number == 3
response = 'Cannot predict now'
elsif number == 4
response = 'My sources say no'
elsif number == 5
response = 'Very doubtful'
end
@prediction = "My prediction is: #{response}.\n"
end
end
#--- app/views/prediction/show.html.erb
<h1>Show Crazy Eight Ball Prediction</h1>
<p> <%= @prediction %< </p>
letters1 = ["a", "b", "c", "d"] letters2 = Array.new letters2 << "a" letters2 << "b" letters2 << "c" letters2 << "d" print letters1, "\n", letters3, "\n"
| Primary Model |
Secondary Model |
Foreign Key |
|---|---|---|
| * Movie | Review | movie_id |
| * Student | Owner | student_id |
| * Pet | Pet | owner_id |
| AirlineFlight | Passenger | airline_flight_id |
| Store | StockItem | store_id |
| SportsTeam | Player | sports_team_id |
a = [2, 6, 4, 5, 1]
# Example 1
b = a.collect do |x|
2 * x
end
print b, "\n"
Ans: [4, 12, 8, 10, 2]
# Example 2
b = (0..5).to_a.collect do |x|
2 * x + 1
end
print b, "\n"
Ans: [1, 3, 5, 7, 9, 11]
# Example 3
b = (1..10).to_a.collect do |x|
[x, x**2]
end
print b, "\n"
Ans: [[1, 1], [2, 4], [3, 9], [4, 16], [5, 25],
[6, 36], [7, 49], [8, 64], [9, 81], [10, 100]]
# 1. Create a range that includes the end using new method. r = Range.new(1, 5) # 2. Shortcut to create a range that includes the end. r = 1..5
vm = VendingMachine.new
(1..6).each do |i|
(1..3).each do |j|
vm.deposit_quarter
end
vm.select_candy
end
<input type="text" name="txtName" size="20" />
<select name="ddmCardType">
<option value="1">Discover</option>
<option value="2">Master Card</option>
<option value="3">Visa</option>
</select>
Check Box:
Fulltime Student<input type="checkbox" name="txtFultimeStatus" /> Fulltime StudentRadio Button: Discover Master Card Visa
<input type="radio" name="cardType" /> Discover &nbnsp; <input type="radio" name="cardType" /> Master Card &nbnsp; <input type="radio" name="cardType" checked="checked" /> Visa
<form name="frmMain" action="summary.php" method="get">
... Controls go here.
</form>
<%= form_tag 'show' do %> ... Controls go here. <% end %>
<%= text_field_tag :cel, @c, :class => 'ctrl' %>
<%= submit_tag 'Convert Temperature', :class => 'ctrl' %>
<%= form_tag 'show' do %> <%= text_field_tag :cel, @cel %> <%= submit_tag 'Convert Temperature' %> <% end %>
input = params[:cel] @c = input.to_iThe String input is then converted to the integer @c.
get "/convert/show"It specifies the route with controller = convert, action = show, and method = get as a named route.
post "/convert/show"
body { background-color: rgb(245,245,255) }
body { background-color: #f5f5ff; }