如何将“if”语句重构为“除非”语句?
有没有办法将此重构为unless
声明?
a = false b = true if !a or !b puts "hello world" end
这似乎并不等同
unless a or b puts "hello world" end
根据De Morgan的法律否定你的状况……
unless (a and b)
那应该是:
puts "hello" unless a and b
要么
unless a and b puts "hello" end
unless
否定if
, unless
你需要否定整个条件表达式(你可以使用De Morgan定律来简化它):
!(!a or !b) ≡ !!a and !!b ≡ a and b
所以:
unless a or b puts "hello world" end