I've just encountered an interesting piece of code. I'd like to determine if it's a ruby standard or a rails convention.
我刚刚遇到了一段有趣的代码。我想确定它是红宝石标准还是铁轨惯例。
redirect_to(session[:return_to] || users_path)
This prevents redirect_to :back from causing errors in some cases. In c++ something like that would mean a function with one bool argument. But here it seems to work another way - it takes that argument which isn't a nil I suppose. Can someone explain it to me and show an example definition of such a function, which takes arguments separated by '||'?
这可以防止redirect_to:在某些情况下导致错误。在c ++中,类似的东西意味着具有一个bool参数的函数。但在这里它似乎以另一种方式工作 - 我认为这个论证不是一个零。有人可以向我解释并显示这样一个函数的示例定义,它将参数分隔为'||'吗?
Bye
4 个解决方案
#1
6
Boolean operators such as ||
and &&
return the last evaluated statement.
布尔运算符,例如||和&&返回最后一个评估语句。
An example,
puts "hi" && "no"
把“hi”&&“no”
The above will print "no" since both strings are considered 'truthy' and "no" was the last value called.
上面将打印“no”,因为两个字符串都被认为是'truthy'而“no”是最后一个被调用的值。
In this case the programmer is taking advantage of the short circuit nature of the ||
operator. So another example,
在这种情况下,程序员正在利用||的短路特性运营商。另一个例子,
puts "hi" || "no"
把“hi”|| “没有”
The above will output "hi" since "hi" is considered truthy and or is short circuited "hi" was the last value called from the expression and will be returned.
上面将输出“hi”,因为“hi”被认为是真实的或者是短路的“hi”是从表达式调用的最后一个值并将被返回。
You will see this kind of construct a lot in ruby, especially with if
s.
你会在ruby中看到这种构造很多,尤其是ifs。
a = if true
"A will be set to this string"
else
"This will never happen"
end
puts a #=> A will be set to this string
Edit: As nick mentioned, it is important to note that only nil
and false
are treated as "falsy". Some interesting examples
编辑:正如尼克所提到的,重要的是要注意只有nil和false被视为“falsy”。一些有趣的例子
a = nil || "hi"
puts a #=> "hi"
a = false || "hi"
puts a #=> "hi"
In these two cases the first argument is "falsy" so the "hi" is evaluated and returned (and then assigned to a)
在这两种情况下,第一个参数是“falsy”,因此“hi”被评估并返回(然后分配给a)
a = Object.new || "hi"
puts a #=> <Object:0x10832420>
In this case (and for any other value as the first argument) Object.new is "true" and thus "hi" is never evaluated. In your particular example, the author was most likely testing for the presence (not nil) of session[:return_to]
. This can be very useful but always remember that it may not work properly if false
is a valid value.
在这种情况下(对于作为第一个参数的任何其他值),Object.new是“true”,因此永远不会评估“hi”。在您的特定示例中,作者最有可能测试会话[:return_to]的存在(而不是nil)。这可能非常有用但始终记住,如果false是有效值,它可能无法正常工作。
#2
3
This is called Short-Circuit Evaluation and it is common in many programming languages
这称为短路评估,在许多编程语言中很常见
In plain English, your statement says
你的陈述用简单的英语说
"redirect to session[:return_to] if it is present, if not, redirect to users_path"
“重定向到会话[:return_to]如果存在,如果不存在,则重定向到users_path”
#3
2
It calls redirect_to(session[:return_to])
if session[:return_to]
is truthy (e.g. not nil
or false
).
如果session [:return_to]是真实的(例如,不是nil或false),则调用redirect_to(session [:return_to])。
If session[:return_to]
is falsy, then it calls redirect_to(users_path)
.
如果session [:return_to]是假的,那么它调用redirect_to(users_path)。
See http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#Logical_Or.
#4
1
The logical OR is short-circuited, meaning that if the left-hand side is "true", or non-nil, then the value of the entire expression is the left-hand side, and the right-hand side is never considered. So if session[:return_to]
is non-nil, it is the value of the expression. If it is nil, though, then the value of the expression is the value of the right-hand side, i.e. the value of user_path
.
逻辑OR是短路的,这意味着如果左侧是“真”或非零,则整个表达式的值是左侧,并且从不考虑右侧。因此,如果session [:return_to]是非nil,则它是表达式的值。但是,如果它是nil,则表达式的值是右侧的值,即user_path的值。
You can even write things like x || x = "foo"
, in which case x
only gets reassigned if it was nil to begin with, but won't be touched if it is non-nil.
你甚至可以写像x ||这样的东西x =“foo”,在这种情况下,如果x开头为n,则仅重新分配x,但如果它是非零则不会被触及。
As for the function, it just takes a string, and it doesn't care what you plug in to the argument.
至于函数,它只需要一个字符串,而不关心你插入参数的内容。
#1
6
Boolean operators such as ||
and &&
return the last evaluated statement.
布尔运算符,例如||和&&返回最后一个评估语句。
An example,
puts "hi" && "no"
把“hi”&&“no”
The above will print "no" since both strings are considered 'truthy' and "no" was the last value called.
上面将打印“no”,因为两个字符串都被认为是'truthy'而“no”是最后一个被调用的值。
In this case the programmer is taking advantage of the short circuit nature of the ||
operator. So another example,
在这种情况下,程序员正在利用||的短路特性运营商。另一个例子,
puts "hi" || "no"
把“hi”|| “没有”
The above will output "hi" since "hi" is considered truthy and or is short circuited "hi" was the last value called from the expression and will be returned.
上面将输出“hi”,因为“hi”被认为是真实的或者是短路的“hi”是从表达式调用的最后一个值并将被返回。
You will see this kind of construct a lot in ruby, especially with if
s.
你会在ruby中看到这种构造很多,尤其是ifs。
a = if true
"A will be set to this string"
else
"This will never happen"
end
puts a #=> A will be set to this string
Edit: As nick mentioned, it is important to note that only nil
and false
are treated as "falsy". Some interesting examples
编辑:正如尼克所提到的,重要的是要注意只有nil和false被视为“falsy”。一些有趣的例子
a = nil || "hi"
puts a #=> "hi"
a = false || "hi"
puts a #=> "hi"
In these two cases the first argument is "falsy" so the "hi" is evaluated and returned (and then assigned to a)
在这两种情况下,第一个参数是“falsy”,因此“hi”被评估并返回(然后分配给a)
a = Object.new || "hi"
puts a #=> <Object:0x10832420>
In this case (and for any other value as the first argument) Object.new is "true" and thus "hi" is never evaluated. In your particular example, the author was most likely testing for the presence (not nil) of session[:return_to]
. This can be very useful but always remember that it may not work properly if false
is a valid value.
在这种情况下(对于作为第一个参数的任何其他值),Object.new是“true”,因此永远不会评估“hi”。在您的特定示例中,作者最有可能测试会话[:return_to]的存在(而不是nil)。这可能非常有用但始终记住,如果false是有效值,它可能无法正常工作。
#2
3
This is called Short-Circuit Evaluation and it is common in many programming languages
这称为短路评估,在许多编程语言中很常见
In plain English, your statement says
你的陈述用简单的英语说
"redirect to session[:return_to] if it is present, if not, redirect to users_path"
“重定向到会话[:return_to]如果存在,如果不存在,则重定向到users_path”
#3
2
It calls redirect_to(session[:return_to])
if session[:return_to]
is truthy (e.g. not nil
or false
).
如果session [:return_to]是真实的(例如,不是nil或false),则调用redirect_to(session [:return_to])。
If session[:return_to]
is falsy, then it calls redirect_to(users_path)
.
如果session [:return_to]是假的,那么它调用redirect_to(users_path)。
See http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#Logical_Or.
#4
1
The logical OR is short-circuited, meaning that if the left-hand side is "true", or non-nil, then the value of the entire expression is the left-hand side, and the right-hand side is never considered. So if session[:return_to]
is non-nil, it is the value of the expression. If it is nil, though, then the value of the expression is the value of the right-hand side, i.e. the value of user_path
.
逻辑OR是短路的,这意味着如果左侧是“真”或非零,则整个表达式的值是左侧,并且从不考虑右侧。因此,如果session [:return_to]是非nil,则它是表达式的值。但是,如果它是nil,则表达式的值是右侧的值,即user_path的值。
You can even write things like x || x = "foo"
, in which case x
only gets reassigned if it was nil to begin with, but won't be touched if it is non-nil.
你甚至可以写像x ||这样的东西x =“foo”,在这种情况下,如果x开头为n,则仅重新分配x,但如果它是非零则不会被触及。
As for the function, it just takes a string, and it doesn't care what you plug in to the argument.
至于函数,它只需要一个字符串,而不关心你插入参数的内容。