如何使用红宝石”情况...当“不平等?
你能在红宝石中做到这一点吗? 它似乎“错过”了不平等的情况
case myvar
when myvar < -5
do somethingA
when -5..-3
do special_something_XX
when -2..-1
do special_something_YY
when myvar == 0
do somethingB
when myvar > 0
go somethingC
end
can you do this in ruby?
it seems to "miss" the cases with inequalities
case myvar
when myvar < -5
do somethingA
when -5..-3
do special_something_XX
when -2..-1
do special_something_YY
when myvar == 0
do somethingB
when myvar > 0
go somethingC
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在混合两种不同类型的 case 语句:
You are mixing two different types of case statements:
我个人并不认为使用 if 语句会更好,但如果您想要这种形式的解决方案:
我的首选解决方案如下。这里顺序很重要,你必须重复
myvar
,但是遗漏情况要困难得多,你不必重复每个边界两次,而且严格性 (<
code> 与<=
而不是..
与...
相比)更为明显。I am not personally convinced that you wouldn't be better off with if statements, but if you want a solution in that form:
My preferred solution follows. Here the order matters and you have to repeat the
myvar
, but it's much harder to leave out cases, you don't have to repeat each bound twice, and the strictness (<
vs<=
rather than..
vs...
) is much more obvious.使用 infinity 可能会有所帮助
Using infinity may help