Ruby 中单独的问号符号和单独的冒号符号是什么意思

What does the lone question mark symbol and lone colon symbol mean in Ruby

在这段之前编写的代码中,有 ? 和一个 : 自己使用(单独的符号),并没有附加到单词上。在这种情况下它们是什么意思?

  # Fill application object with applicant info; generic info if not provided.
  availability ? formdata[:availability] = availability : (formdata[:availability] = [0,1,2,3,4,5,6])

这是ternary operator。它相当于 if/else。您的代码也可以这样写:

if availability 
  formdata[:availability] = availability 
else
  (formdata[:availability] = [0,1,2,3,4,5,6])
end

我还认为它 应该 简化为这样的东西:

formdata[:availability] = availability ? availability : [0,1,2,3,4,5,6]

条件语句中的赋值是一种奇怪的写法,并不常见。

您还可以将其进一步简化为:

formdata[:availability] = availability || [0,1,2,3,4,5,6]

how an || (or) assignment operates can be found here

的详情