Ruby Conditions - if, case, etc.

Conditions Fundamental

Operators

Condition Operator Keyword
And &&
And and
Or ||
Or or
Not !
Not not

If Statement

 1 do_print = true
 2 
 3 # Style
 4 if do_print then
 5   print "Hello" 
 6 end
 7 
 8 # Style with :
 9 # TEST IT
10 # if do_print : print "Hello" end
11 
12 # Sytle by putting if condition at the end
13 print "Hello" if do_print
14 
15 # Ternary style
16 do_print == true ? "Yes" : "No"

else and elseif

1 state = "red"
2 
3 if state == "red"
4   print "Failed"
5 elseif state == "yellow"
6   print "Work in progress:
7 else
8   print "Good to go"
9 end

Case satetment

 1 # Style
 2 my_state = "red"
 3 my_status = case state
 4   when "red" then "failed"
 5   when "yellow" then "in progress"
 6   when "green" then "passed"
 7   else "unknown"
 8 end
 9 print my_status
10 
11 # Range casing
12 my_score = 90
13 case my_score
14   when 0-60 then print "Failed"
15   when 61..100 then print "Passed"
16 end