Ruby:条件矩阵?具有多个条件的情况?

发布于 2024-10-19 06:17:31 字数 423 浏览 2 评论 0原文

在红宝石中,我想知道是否有办法执行以下操作:

我基本上有四个可能结果的矩阵:

A is True, B is True
A is True, B is False
A is False, B is True
A is False, B is False

我想以最干净的“红宝石方式”为此编写一个测试。

我希望做一些类似的事情

case[A,B]
  when A && B then ...
  when A && !B then ...
  when !A && B then ...
  when !A && !B then ...
end

...但这行不通。那么,处理这种情况的最佳方法是什么?

In ruby, I was wondering if there's a way to do the following:

I have basically a matrix of four possible outcomes:

A is True, B is True
A is True, B is False
A is False, B is True
A is False, B is False

I'd like to write a test for this in the cleanest possible "ruby way".

I was hoping to do something like

case[A,B]
  when A && B then ...
  when A && !B then ...
  when !A && B then ...
  when !A && !B then ...
end

... but that doesn't work. So, what's the best way to handle this kind of situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

放手` 2024-10-26 06:17:31

布尔情况(case 中没有表达式,它返回第一个带有 truthy when_expr 的分支):

result = case
when A && B then ...
when A && !B then ...
when !A && B then ...
when !A && !B then ...
end

匹配情况(case 中带有表达式) case,它返回满足谓词when_expr === case_expr的第一个分支:

result = case [A, B]
when [true, true] then ...
when [true, false] then ...
when [false, true] then ...
when [false, false] then ...
end

Boolean case (with no expression in the case, it returns the first branch with a truthy when_expr):

result = case
when A && B then ...
when A && !B then ...
when !A && B then ...
when !A && !B then ...
end

Matching case (with an expression in the case, it returns the first branch that satisfies the predicate when_expr === case_expr):

result = case [A, B]
when [true, true] then ...
when [true, false] then ...
when [false, true] then ...
when [false, false] then ...
end
疏忽 2024-10-26 06:17:31

如果您正在寻找具有一个条件但多个匹配器的案例..

case @condition
  when "a" or "b"
    # do something
  when "c"
    # do something
end

..那么您实际上需要这个

case @condition
  when "a", "b"
    # do something
  when "c"
    # do something
end

这可以重写为

case @condition
  when ("a" and "b")
    # do something
  when "c"
    # do something
end

但这有点违反直觉,因为它相当于

if @condition == "a" or @condition == "b"

If you're looking for a case with one condition but multiple matchers..

case @condition
  when "a" or "b"
    # do something
  when "c"
    # do something
end

..then you actually need this:

case @condition
  when "a", "b"
    # do something
  when "c"
    # do something
end

This can be rewritten as

case @condition
  when ("a" and "b")
    # do something
  when "c"
    # do something
end

But this is somewhat counter-intuitive, as it's equivalent to

if @condition == "a" or @condition == "b"
亣腦蒛氧 2024-10-26 06:17:31

不确定是否有标准的 Ruby 方式,但你总是可以将它们转换为数字:

val = (a ? 1 : 0) + (b ? 2 : 0)
case val
  when 0 then ...
  when 1 then ...
  when 2 then ...
  when 3 then ...
end

或者有一个 procs 数组的数组,然后执行

my_procs[a][b].call()

Not sure if there's a standard Ruby way, but you can always turn them into a number:

val = (a ? 1 : 0) + (b ? 2 : 0)
case val
  when 0 then ...
  when 1 then ...
  when 2 then ...
  when 3 then ...
end

or have an array of arrays of procs and do

my_procs[a][b].call()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文