Can anyone explain me the difference between if
and unless
and when to use it?
The difference between if
and unless
is that they are exact opposites of each other:
if
takes a condition, a then-block and an optional else-block, and it evaluates the then-block if the condition is truthy, otherwise it evaluates the else-blockunless
takes a condition, a then-block and an optional else-block, and it evaluates the then-block if the condition is falsy, otherwise it evaluates the else-blockOr, in other words: they mean pretty much the same thing in Ruby as they do in English.