This question already has an answer here:
这个问题在这里已有答案:
- How do I use the conditional operator (? :) in Ruby? 6 answers
如何在Ruby中使用条件运算符(?:)? 6个答案
What does the line below checks and perform?
下面的行检查和执行什么?
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
Here is the block that contains the line of code.
这是包含代码行的块。
def some_name(root_dir = nil, environment = 'stage', branch)
prefix = root_dir.nil? ? nil : File.join(root_dir, '/')
.
.
.
i know that the '?' in ruby is something that checks the yes/no fulfillment. But I am not very clear on its usage/syntax in the above block of code.
我知道'?'在红宝石中是检查是/否履行的东西。但我对上面的代码块中的用法/语法不是很清楚。
3 个解决方案
#1
Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.
最终的功能? Ruby中的函数只返回一个布尔值,即true或false。
当您编写只能返回true或false的函数时,您应该使用问号结束函数名称。
The example you gave shows a ternary statement, which is a one-line if-statement. .nil?
is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil?
function returns true, it gets nil as value, else it gets the File.join(root_dir, '/')
as value.
您给出的示例显示了一个三元语句,它是一行if语句。 。零?是一个布尔函数,如果值为n则返回true,否则返回false。它首先检查函数是否为true或false。然后执行if / else来赋值(如果.nil?函数返回true,它将nil作为值,否则它将File.join(root_dir,'/')作为值。
It can be rewritten like so:
它可以像这样重写:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
#2
This is called a ternary operator and is used as a type of shorthands for if/else statements. It follows the following format
这称为三元运算符,用作if / else语句的一种缩写。它遵循以下格式
statement_to_evaluate ? true_results_do_this : else_do_this
A lot of times this will be used for very short or simple if/else statements. You will see this type of syntax is a bunch of different languages that are based on C.
很多时候,这将用于非常简短的if / else语句。您将看到这种语法是一堆基于C语言的不同语言。
#3
The code is the equivalent of:
代码相当于:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
查看上一个问题
#1
Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false.
最终的功能? Ruby中的函数只返回一个布尔值,即true或false。
当您编写只能返回true或false的函数时,您应该使用问号结束函数名称。
The example you gave shows a ternary statement, which is a one-line if-statement. .nil?
is a boolean function that returns true if the value is nil and false if it is not. It first checks if the function is true, or false. Then performs an if/else to assign the value (if the .nil?
function returns true, it gets nil as value, else it gets the File.join(root_dir, '/')
as value.
您给出的示例显示了一个三元语句,它是一行if语句。 。零?是一个布尔函数,如果值为n则返回true,否则返回false。它首先检查函数是否为true或false。然后执行if / else来赋值(如果.nil?函数返回true,它将nil作为值,否则它将File.join(root_dir,'/')作为值。
It can be rewritten like so:
它可以像这样重写:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
#2
This is called a ternary operator and is used as a type of shorthands for if/else statements. It follows the following format
这称为三元运算符,用作if / else语句的一种缩写。它遵循以下格式
statement_to_evaluate ? true_results_do_this : else_do_this
A lot of times this will be used for very short or simple if/else statements. You will see this type of syntax is a bunch of different languages that are based on C.
很多时候,这将用于非常简短的if / else语句。您将看到这种语法是一堆基于C语言的不同语言。
#3
The code is the equivalent of:
代码相当于:
if root_dir.nil?
prefix = nil
else
prefix = File.join(root_dir, '/')
end
查看上一个问题