问号和冒号 - 如果在红宝石中

时间:2021-09-18 01:20:19

Hi I have a question about 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,产品将按“name”排序。但是,我不明白这个陈述的写法,特别是第二个“?”。有人可以向我解释一下吗?

4 个解决方案

#1


14  

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).

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

#2


10  

?: is a ternary operator that 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

#3


4  

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的简写。

#4


2  

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).

第二个问号和冒号是三元操作数的一部分:(如果这是真的)? (这样做):(否则,这样做)。

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

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

#1


14  

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).

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

#2


10  

?: is a ternary operator that 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

#3


4  

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的简写。

#4


2  

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).

第二个问号和冒号是三元操作数的一部分:(如果这是真的)? (这样做):(否则,这样做)。

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

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