在书上看到
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(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)
簡單。