Ruby - Operator Boolean unless

Introduction

Ruby can perform unless tests, which are the exact opposite of if tests:

Demo

aDay = "Monday"
unless aDay == 'Saturday' or aDay == 'Sunday' 
   daytype = 'weekday' 
else #   w  w w .j  a va 2 s . com
   daytype = 'weekend' 
end

unless is an alternative way of expressing "if not."

The following is equivalent to the previous code; both consider Saturday and Sunday to be the weekend and other days to be weekdays:

Demo

aDay = "Monday"
if !(aDay == 'Saturday' or aDay == 'Sunday') 
   daytype = 'weekday' 
else #   w ww. j  a va 2  s.  c  om
   daytype = 'weekend' 
end

Related Topic