A simple if test has only one of two possible results.
Either a bit of code is run or it isn't, depending on whether the test evaluates to true or not.
Often, you will need to have more than two possible outcomes.
Ruby uses one equal sign (=) to assign a value and two (==) to test a value.
aDay = 'Monday' if aDay == 'Saturday' or aDay == 'Sunday' daytype = 'weekend' else # w w w.jav a 2s . c o m daytype = 'weekday' end
The if condition here is straightforward.
It tests two possible conditions: if the value of the variable aDay is equal to the string "Saturday" and if the value of aDay is equal to the string "Sunday."
If either of those conditions is true, then the next line of code executes daytype = 'weekend'; in all other cases, the code after else executes daytype = 'weekday'.
When an if test and the code to be executed are placed on separate lines, the then keyword is optional.
When the test and the code are placed on a single line, the then keyword is obligatory:
x = 1 if x == 1 then puts( 'ok' ) end #if x == 1 puts( 'ok' ) end # syntax error! # from w w w.ja v a2s .c o m