问号和冒号 - 如果在 ruby​​ 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11191247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:35:42  来源:igfitidea点击:

question mark and colon - if else in ruby

ruby-on-railsrubyternary-operator

提问by u19964

Hi I have a question about ruby on rails

嗨,我有一个关于 ruby​​ on rails 的问题

Apparently I have a statement like this:

显然我有这样的声明:

def sort_column
    Product.column_names.include?(params[:sort]) ? params[:sort] : "name"
end

From what I read, it's said that this method sort the column based on params[:sort] and if there no params the products will be sorted by "name". However, I don't understand the way this statement is written, especially the second "?". Can someone explain it to me ?

从我读到的内容来看,据说此方法根据 params[:sort] 对列进行排序,如果没有 params,则产品将按“名称”排序。但是,我不明白这个语句的写法,尤其是第二个“?”。有人可以向我解释一下吗?

回答by Sergio Tulentsev

This is your code, rearranged for easier understanding.

这是您的代码,为了更容易理解而重新排列。

def sort_column
  cond = Product.column_names.include?(params[:sort]) 
  cond ? params[:sort] : "name"
  #  it's equivalent to this
  # if cond
  #   params[:sort]
  # else
  #   'name'
  # end
end

First question mark is part of a method name, the second one - part of ternary operator (which you should read about).

第一个问号是方法名称的一部分,第二个 - 三元运算符的一部分(您应该阅读)。

回答by Matheus Moreira

?:is a ternary operatorthat is present in many languages. It has the following syntax:

?:存在于许多语言中的三元运算符。它具有以下语法:

expression ? value_if_true : value_if_false

In Ruby, it is a shorter version of this:

在 Ruby 中,它是一个较短的版本:

if expression
  value_if_true
else
  value_if_false
end

回答by LanceH

That line translates roughly as:

该行大致翻译为:

if Product.column_names.include?(params[:sort])
    params[:sort]
else
    "name"
end

The ? : is a ternary operator; shorthand for a brief if-else.

这 ?: 是一个三元运算符;简短的 if-else 的简写。

回答by André Medeiros

Product.column_names.include?(params[:sort]) ? params[:sort] : "name"

The first question mark is part of the method name: include?.

第一个问号是方法名称的一部分:include?

The second question mark and the colon are part of the ternary operand: (if this is true) ? (do this) : (else, do that).

第二个问号和冒号是三元操作数的一部分: (if this is true) ? (do this) : (else, do that).

It means that, if Product.column_namescontains params[:sort], it will return params[:sort]. Else, it will return "name".

这意味着,如果Product.column_names包含params[:sort],它将返回params[:sort]。否则,它将返回"name"

回答by Cris R

We must to be careful with the expression part to be evaluated in the ternary operator, for instance when using andversus &&, this is what could happen:

我们必须小心在三元运算符中要计算的表达式部分,例如在使用andvs 时&&,可能会发生以下情况:

2.6.2 :014 > a = false
 => false
2.6.2 :015 > b = true
 => true
2.6.2 :016 > a and b ? 'first' : 'second'
 => false
2.6.2 :017 > a && b ? 'first' : 'second'
 => "second"
2.6.2 :018 > (a and b) ? 'first' : 'second'
 => "second"