如何使用红宝石”情况...当“不平等?

发布于 2024-10-19 02:12:24 字数 272 浏览 2 评论 0原文

你能在红宝石中做到这一点吗? 它似乎“错过”了不平等的情况

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

停顿的约定 2024-10-26 02:12:24

您正在混合两种不同类型的 case 语句:

case var
when 1
  dosomething
when 2..3
  doSomethingElse
end

case
when var == 1
   doSomething
when var < 12
   doSomethingElse
end

You are mixing two different types of case statements:

case var
when 1
  dosomething
when 2..3
  doSomethingElse
end

case
when var == 1
   doSomething
when var < 12
   doSomethingElse
end
爱的那么颓废 2024-10-26 02:12:24
   case myvar
     when  proc { |n| n < -5 }
        do somethingA
     when -5..-3
        do special_something_XX
     when -2..-1
        do special_something_YY
     when proc { |n| n == 0 }
        do somethingB
     when proc { |n| n > 0 }
        go somethingC
     end
   end
   case myvar
     when  proc { |n| n < -5 }
        do somethingA
     when -5..-3
        do special_something_XX
     when -2..-1
        do special_something_YY
     when proc { |n| n == 0 }
        do somethingB
     when proc { |n| n > 0 }
        go somethingC
     end
   end
站稳脚跟 2024-10-26 02:12:24

我个人并不认为使用 if 语句会更好,但如果您想要这种形式的解决方案:

Inf = 1.0/0

case myvar
when -Inf..-5
  do somethingA
when -5..-3
  do special_something_XX
when -2..-1
  do special_something_YY
when 0
  do somethingB
when 0..Inf
  do somethingC
end

我的首选解决方案如下。这里顺序很重要,你必须重复 myvar,但是遗漏情况要困难得多,你不必重复每个边界两次,而且严格性 (< code> 与 <= 而不是 ..... 相比)更为明显。

if myvar <= -5
  # less than -5
elsif myvar <= -3
  # between -5 and -3
elsif myvar <= -1
  # between -3 and -1
elsif myvar <= 0
  # between -1 and 0
else
  # larger than 0
end

I am not personally convinced that you wouldn't be better off with if statements, but if you want a solution in that form:

Inf = 1.0/0

case myvar
when -Inf..-5
  do somethingA
when -5..-3
  do special_something_XX
when -2..-1
  do special_something_YY
when 0
  do somethingB
when 0..Inf
  do somethingC
end

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.

if myvar <= -5
  # less than -5
elsif myvar <= -3
  # between -5 and -3
elsif myvar <= -1
  # between -3 and -1
elsif myvar <= 0
  # between -1 and 0
else
  # larger than 0
end
赠意 2024-10-26 02:12:24
def project_completion(percent)
 case percent
  when  percent..25
    "danger"
  when percent..50
    "warning"
  when percent..75
    "info"
  when percent..100
    "success"
  else
   "info"
  end
end
def project_completion(percent)
 case percent
  when  percent..25
    "danger"
  when percent..50
    "warning"
  when percent..75
    "info"
  when percent..100
    "success"
  else
   "info"
  end
end
情愿 2024-10-26 02:12:24

使用 infinity 可能会有所帮助

case var
when -Float::INFINITY..-1
when 0
when 1..2
when 3..Float::INFINITY
end

Using infinity may help

case var
when -Float::INFINITY..-1
when 0
when 1..2
when 3..Float::INFINITY
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文