if expression
...
elsif expression
...
else
...
end
unless 는 elsif 가 없다.
unless expression
...
else
...
end
한 줄짜리도 가능
"Hello" if i_am_nice
"Go away" else
case
when expression
...
when expression
...
else
...
end
case test_value
when value
...
when value
...
else
...
end
> boolean ? result1 : result2
# y 값이 있으면 y, 없다면 z를 x에 할당.
> x = y || z
# x 값이 없으면 x에 y값을 할당
> x ||= y
제어 키워드
i = 5
loop do
break if i <= 0
puts "Countdown: #{i}"
i -= 1
end
puts "Blast off!"
while boolean
...
end
until boolean
...
end
이터레이터를 이용한 반복
5.times do
puts "Hello !"
end
1.upto(5) { puts "Hello" }
5.downto(1) { puts "Hello" }
(1..5).each { puts "Hello" }
"stirng\nstring\n".each_line {|line| puts line }
"stirng".each_char {|char| puts char }
"stirng".each_byte {|byte| puts byte }
array.each
array.each_index
array.each_with_index
hash.each
hash.each_key
hash.each_value
hash.each_pair
for fruit in ["banana", "apple", "pear"]
puts fruit.capitalize
end