unless 语句的用法刚好与 if 语句相反。 unless 语句的用法如下: unless 条件 then 处理 end ※ 可以省略 then unless 语句的形式和 if 语句一样。但 if 语句是条件为真时执行 处理, unless 语句则刚好相反,条件为假时执行处理。 下面是使用 unless 的例子(代码清单 5.3)。 这个程序执行后输出“a 不比 b 大 ”。unless 语句的条件 a > b 为假,所以程序执行 了 puts 方法。 unless 语句也可以使用 else 。 unless 条件 处理 1 else 处理 2 end 这个与下面的 if 语句是等价的。 if 条件 处理 2 else 处理 1 end 对比以上两种写法,我们可以知道处理 1 和处理 2 的位置互换了,if 语句通过这样的互换, 能达到与使用 unless 语句时同样的效果。