我想要做这件事:
case cost
when cost between 1 and 3 then cost * 1.1
when cost between 3 and 5 then cost * 1.2
else
0
我想要做这件事:
case cost
when cost between 1 and 3 then cost * 1.1
when cost between 3 and 5 then cost * 1.2
else
0
是的,因为Range#===
被定义为与include?
相同,所以您可以在case
语句中使用范围:
case cost
when 1..3 then cost * 1.1
when 3..5 then cost * 1.2
case expression
when min..max
statements
else
statements
end
..
时,两个操作数的范围都是包含的(当使用...
时,右操作数是排除在外的),并且case
会匹配第一个符合条件的模式。 - sepp2k