learn_ruby_ch3_pt2 Flashcards
Ternary operator (argument, arguments)
label = length == 1 ? “ argument” : “ arguments”
case statement
lang = “fr”
lang = “fr”
dog = case lang when "en" "dog" when "es" "perro" when "fr" "chien" else "dog" end
colon form no longer supported starting in 1.9, e.g.,
when “en”: “dog”
while array loop (breeds)
i = 0
breeds = [ “quarter”, “arabian”, “appalosa”, “paint” ]
puts breeds.size # => 4
temp = []
while i < breeds.size do
temp «_space;breeds[i].capitalize
i +=1
end
temp.sort! # => [“Appalosa”, “Arabian”, “Paint”, “Quarter”]
breeds.replace( temp )
p breeds # => [“Appalosa”, “Arabian”, “Paint”, “Quarter”]
The “do” is optional
begin while loop (temp)
temp = 98.3
begin
print “Temp is “ + temp.to_s + “ Fahrenheit. “
temp += 0.1
end while temp < 98.6
while with break (breeds)
i = 0 breeds = [ "quarter", "arabian", "appalosa", "paint" ] temp = []
while i < breeds.size temp << breeds[i].capitalize break if temp[i] == "Arabian" i +=1 end
p temp # => [“Quarter”, “Arabian”]