Conditionals are keywords that execute a certain statement under a specific condition.
if language == 'Ruby' [then] 'awesome' end
Several conditions case
if language == 'Ruby' && framework == 'Ruby on Rails' [then] 'awesome' end
Short syntax
'awesome' if language == 'Ruby'
if language == 'Ruby' [then] 'awesome' else 'not so good' end
Short syntax (ternary operator)
language == 'Ruby' ? 'awesome' : 'not so good'
if language == 'Ruby' [then] 'awesome' elsif language == 'Python' [then] 'so so' else 'not so good' end
Short syntax
if language == 'Ruby' then 'awesome' elsif language == 'Python' then 'so so' else 'not so good' end
unless language == 'Ruby' [then] 'not so good' end
Short syntax
'not so good' unless language == 'Ruby'
unless language == 'Ruby' [then] 'not so good' else 'awesome' end
case language when 'Ruby' 'awesome' when 'Python' 'so so' else 'not so good' end
case year when 1800...1900 then 'XIX century' when 1900...2000 then 'XX century' when 2000...2100 then 'XXI century' end
Loops are blocks of program that are executed multiple times.
loop do # block end
i = 0 while i < 10 do print i i += 1 end
Short syntax
i = 200 i -= 10 while i >= 100
i = 0 until i >= 10 do print i i += 1 end
Short syntax
i = 200 i -= 10 until i < 100
for i in 1..5 print i, ' ' end
i=0 loop do i += 1 next if i < 3 print i break if i > 4 end
0.upto(9) { |x| print x, ' ' } => "0 1 2 3 4 5 6 7 8 9 "
9.downto(0) { |x| print x, ' ' } => "9 8 7 6 5 4 3 2 1 0 "
3.times { print 'Ho ' } => "Ho Ho Ho "
0.step(12, 3) { |x| print x, ' ' } => "0 3 6 9 12 "
[1, 2, 3, 5, 10].each { |n| print n, ' ' } => "1 2 3 5 10 "
Created by Vasyl Lasiak / @vlasiak