gpt4 book ai didi

ruby - ruby 是否支持 case 语句中的范围?

转载 作者:数据小太阳 更新时间:2023-10-29 06:23:06 25 4
gpt4 key购买 nike

我想这样做:

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

关于ruby - ruby 是否支持 case 语句中的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4130168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com