tutorialspoint
:- 说
case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end
比较使用操作符时expression
指定的case
和指定的,===
并执行when
匹配的子句的代码。
说,看下面:
A = "am"
F = "fm"
L = "dd"
B = 'aa'
def fmam(n)
return if n == 0
case true
when n % 15 == 0
puts B + L
when n % 5 == 0 # this evaluates to true first, which then matched with true value mentioned in the case statement.
puts L
when n % 3 == 0
puts B
else
puts n
end
end
fmam(20) #=> dd
现在看下面的代码:
A = "am"
F = "fm"
L = "dd"
B = 'aa'
def fmam(n)
return if n == 0
case false
when n % 25 == 0 # this evaluates to false first, which then matched with false value mentioned in the case statement.
puts B + L
when n % 5 == 0
puts L
when n % 3 == 0
puts B
else
puts n
end
end
fmam(30) #=> aadd