switch case语句_使用Case(Switch)Ruby语句

switch case语句

In most computer languages, the case or conditional (also known as switch) statement compares the value of a variable with that of several constants or literals and executes the first path with a matching case. In Ruby, it's a bit more flexible (and powerful).

在大多数计算机语言中 ,case或条件语句(也称为switch )将变量的值与几个常量或文字的值进行比较,并以匹配的case执行第一个路径。 在Ruby中 ,它更加灵活(功能强大)。

Instead of a simple equality test being performed, the case equality operator is used, opening the door to many new uses.

代替执行简单的相等性测试,而是使用案例相等性运算符,这为许多新用途打开了大门。

There are some differences from other languages though. In C, a switch statement is a kind of replacement for a series of if and goto statements. The cases are technically labels, and the switch statement will go to the matching label. This exhibits a behavior called "fallthrough," as the execution doesn't stop when it reaches another label.

但是与其他语言有些区别。 在C中 ,switch语句是一系列if和goto语句的一种替代。 这些案例在技术上是标签,并且switch语句将转到匹配的标签。 这表现出一种称为“ fallthrough”的行为,因为当到达另一个标签时执行不会停止。

This is usually avoided using a break statement, but fallthrough is sometimes intentional. The case statement in Ruby, on the other hand, can be seen as a shorthand for a series of if statements. There is no fallthrough, only the first matching case will be executed.

通常使用break语句可以避免这种情况,但是有时会故意失败。 另一方面,Ruby中的case语句可以看作一系列if语句的简写。 没有失败,只会执行第一个匹配的大小写。

案件陈述书的基本形式 ( The Basic Form of a Case Statement )

The basic form of a case statement is as follows.

case语句的基本形式如下。

As you can see, this is structured something like an if/else if/else conditional statement. The name (which we'll call the value), in this case inputted from the keyboard, is compared to each of the cases from the when clauses (i.e. cases), and the first when block with a matching case will be executed. If none of them match, the else block will be executed.

如您所见,它的结构类似于if / else if / else条件语句。 在这种情况下,将从键盘输入的名称(我们将其称为value )与when子句(例如, case )中的每个case进行比较,并将执行第一个带有匹配case的when块。 如果它们都不匹配,则将执行else块。

What's interesting here is how the value is compared to each of the cases. As mentioned above, in C++, and other C-like languages, a simple value comparison is used. In Ruby, the case equality operator is used.

这里有趣的是如何将值与每种情况进行比较。 如上所述,在C ++和其他类似C的语言中,使用了简单的值比较。 在Ruby中,使用大小写相等运算符。

Remember that the type of the left-hand side of a case equality operator is important, and the cases are always the left-hand side. So, for each when clause, Ruby will evaluate case === value until it finds a match.

请记住,案例相等性运算符左侧的类型很重要,案例始终为左侧。 因此,对于每个when子句,Ruby都会评估case ===的值,直到找到匹配项为止。

If we were to input Bob, Ruby would first evaluate "Alice" === "Bob", which would be false since String#=== is defined as the comparison of the strings. Next, /[qrz].+/i === "Bob" would be executed, which is false since Bob doesn't begin with Q, R or Z.

如果我们要输入Bob ,那么Ruby首先会评估“ Alice” ===“ Bob” ,因为String#===被定义为字符串的比较,所以它是错误的。 接下来,将执行/[qrz].+/i ===“ Bob” ,这是错误的,因为Bob的开头不是Q,R或Z。

Since none of the cases matched, Ruby will then execute the else clause.

由于所有情况都不匹配,因此Ruby将执行else子句。

类型如何发挥作用 ( How the Type Comes Into Play )

A common use of the case statement is to determine the type of value and do something different depending on its type. Though this breaks Ruby's customary duck typing, it's sometimes necessary to get things done.

case语句的常见用法是确定值的类型,并根据其类型执行不同的操作。 尽管这打破了Ruby惯用的鸭子输入方式,但有时还是有必要将其完成。

This works by using the Class#=== (technically, the Module#===) operator, which tests if the right-hand side is_a? left-hand side.

这可以通过使用Class#=== (在技术上为Module#==== )运算符来实现,该运算符将测试右侧的is_a吗? 左手边。

The syntax is simple and elegant:

语法简单而优雅:

另一种可能的形式 ( Another Possible Form )

If the value is omitted, the case statement works a bit differently: it works almost exactly like an if/else if/else statement. The advantages of using the case statement over an ​if statement, in this case, are merely cosmetic.

如果省略该 ,则case语句的工作方式略有不同:它的工作方式几乎完全类似于if / else if / else语句。 使用case语句在一个if语句,在这种情况下的优点,仅仅是化妆品。

更紧凑的语法 ( A More Compact Syntax )

There are times when there are a large number of small when clauses. Such a case statement easily grows too large to fit on the screen. When this is the case (no pun intended), you can use the then keyword to put the body of the when clause on the same line.

有时候,会有大量的小when子句。 这样的案例陈述很容易变得太大而无法容纳在屏幕上。 在这种情况下(不需要双关语),可以使用then关键字将when子句的正文放在同一行上。

While this makes for some very dense code, as long as each when clause is very similar, it actually becomes more readable.

尽管这需要一些非常密集的代码,但只要每个when子句非常相似,它实际上就会变得更具可读性。

When you should use single-line and multi-line when clauses are up to you, it's a matter of style. However, mixing the two is not recommended - a case statement should follow a pattern to be as readable as possible.

当您应该使用单行和多行的when子句时,这是样式问题。 但是,不建议将两者混为一谈-case语句应遵循一定的模式,以使其更具可读性。

案例分配 ( Case Assignment )

Like if statements, case statements evaluate to the last statement in the when clause. In other words, they can be used in assignments to provide a kind of table. However, don't forget that case statements are much more powerful than simple array or hash lookups. Such a table doesn't necessarily need to use literals in the when clauses.

与if语句一样,case语句的计算结果为when子句中的最后一条语句。 换句话说,它们可以用于分配中以提供一种表。 但是,不要忘记case语句比简单的数组或哈希查找功能强大得多。 这样的表不一定需要在when子句中使用文字。

If there is no matching when clause and no else clause, then the case statement will evaluate to nil.

如果没有匹配的when子句和else子句,则case语句的计算结果为nil

翻译自: https://www.thoughtco.com/case-switch-statement-2907913

switch case语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值