tutorialspoint
: - 說
case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end
比較由case
指定的expression
和使用===
操作時指定並執行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