Ruby: If Then Else
Ruby “if” is a expression. It returns the last value of the executed block.
Tip: “everything” in Ruby is a expression. They return a value.
xx = 4 p(if xx > 0 then true end) # formatted in multiple lines p(if xx > 0 then true end)
Example of “if else”:
# example of if else y = 2 p((if y == 1 then 3 else 4 end) == 4)
Example of “if else” chain:
# example of if else chain z = 2 p(( if z < 0 then 'neg' elsif z == 0 then 'zero' elsif z == 1 then 'one' else 'other' end ) == "other")
Short Form of If Expression
# short form p( 3 > 2 ? true : false)