我想做这个:
case cost
when cost between 1 and 3 then cost * 1.1
when cost between 3 and 5 then cost * 1.2
else
0
Run Code Online (Sandbox Code Playgroud)
sep*_*p2k 61
是的,因为Range#===定义为相同include?,您可以在case语句中使用范围:
case cost
when 1..3 then cost * 1.1
when 3..5 then cost * 1.2
Run Code Online (Sandbox Code Playgroud)
是的。我不知道你为什么不考虑谷歌这个或者只是尝试一下(这是 Ruby 的美妙之处,IMO:事情通常按照你认为应该的方式工作),但我会回答同样的问题:http: //ilikestuffblog.com/2008/04/15/how-to-write-case-switch-statements-in-ruby/
具体来说:
case expression
when min..max
statements
else
statements
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13967 次 |
| 最近记录: |