在书上看到
if(x<5) then statement1 endunless x>=5 then statement1 end
这两句是对等的
if x<5 then statement1
else
statement2
endunless x<5 then statement2
else
statement1
end
也对等
正常來說, if比較用多。
unless即是if not,可以省卻一個key。
Ruby容許寫出
a = 5 unless b > 10
比
a = 5 if not (b > 10)
簡單。
上有錯字,是keyword,不是key。今更正。