Ruby 判断
Ruby 提供了几种很常见的条件结构。在这里,我们将解释所有的条件语句和 Ruby 中可用的修饰符。
Ruby if...else 语句
语法
if 表达式用于条件执行。值 false 和 nil 为假,其他值都为真。请注意,Ruby 使用 elsif,不是使用 else if 和 elif。
如果 conditional 为真,则执行 code。如果 conditional 不为真,则执行 else 子句中指定的 code。
通常我们省略保留字 then 。若想在一行内写出完整的 if 式,则必须以 then 隔开条件式和程式区块。如下所示:
实例
以上实例输出结果:
x 是 1
Ruby if 修饰符
语法
if修饰词组表示当 if 右边之条件成立时才执行 if 左边的式子。即如果 conditional 为真,则执行 code。
实例
以上实例输出结果:
debug
Ruby unless 语句
语法
unless式和 if式作用相反,即如果 conditional 为假,则执行 code。如果 conditional 为真,则执行 else 子句中指定的 code。
实例
以上实例输出结果为:
x 小于 2
Ruby unless 修饰符
语法
如果 conditional 为假,则执行 code。
实例
以上实例输出结果:
1 -- 这一行输出 3 -- 这一行输出
Ruby case 语句
语法
case先对一个 expression 进行匹配判断,然后根据匹配结果进行分支选择。
它使用 ===运算符比较 when 指定的 expression,若一致的话就执行 when 部分的内容。
通常我们省略保留字 then 。若想在一行内写出完整的 when 式,则必须以 then 隔开条件式和程式区块。如下所示:
因此:
基本上类似于:
实例
以上实例输出结果为:
小孩
当case的"表达式"部分被省略时,将计算第一个when条件部分为真的表达式。
点击查看所有 Ruby 教程 文章: https://www.codercto.com/courses/l/16.html
C++ Concurrency in Action
Anthony Williams / Manning Publications / 2012-2-28 / USD 69.99
HIGHLIGHT C++ Concurrency in Action is the first book to market to show how to take advantage of the new C++ Standard and how to write robust multi-threaded applications in C++. DESCRIPTION With ......一起来看看 《C++ Concurrency in Action》 这本书的介绍吧!