The following code results in an error
以下代码导致错误
Example 1
例1
if params[:id] == '2' || params.has_key? :id
abort('params id = 2 or nothing')
end
syntax error, unexpected tSYMBEG, expecting keyword_then or ';' or '\n'
if params[:id] == '2' || params.has_key? :id
However, switching the conditional statements || adding parentheses works 100%.
但是,切换条件语句||添加括号可以100%工作。
Example 2
例2
if params.has_key? :id || params[:id] == '2'
abort('params id = 2 or nothing')
end
Example 3
例3
if (params[:id] == '2') || (params.has_key? :id)
abort('params id = 2 or nothing')
end
Can anyone explain to me why Example 1 will result in an error ?
任何人都可以向我解释为什么示例1会导致错误?
Thanks
谢谢
4 个解决方案
#1
4
Your problem is happening at:
你的问题发生在:
params[:id] == '2' || params.has_key? :id
which can be simplified to:
可以简化为:
:foo || some_method :bar
which causes the same error. This expression is in principle, ambiguous between
这会导致同样的错误。这个表达原则上是模糊的
(:foo || some_method) :bar (1)
and
和
:foo || (some_method :bar) (2)
When an expression is ambiguous, it is resolved by other factors. One factor, operator precedence tells nothing here about disambiguating between (1) and (2). The next factor is linear order. Since ||
appears before argument application ()
(omitted) in the expression in question, the former applies before the latter. Therefore, the expression is interpreted as (1). Since (:foo || some_method)
would then be parsed as an expression, there would be two expressions next to each other. That is ungrammatical, just as:
当表达式不明确时,它会被其他因素解决。一个因素,运算符优先级在这里没有说明消除(1)和(2)之间的歧义。下一个因素是线性顺序。自||出现在有问题的表达式中的参数application()(省略)之前,前者适用于后者。因此,表达式被解释为(1)。由于(:foo || some_method)将被解析为表达式,因此会有两个表达式彼此相邻。这是不合语法的,就像:
:baz :bar
is ungrammatical.
是不合语法的。
In fact, if you switch the order as:
实际上,如果您将订单切换为:
some_method :bar || :foo
then, it will be interpreted as
然后,它将被解释为
(some_method :bar) || :foo
for the same reason, and syntax error will disappear.
出于同样的原因,语法错误将消失。
Also when you resolve ambiguity by explicitly using parentheses to indicate argument application:
此外,当您通过显式使用括号来指示参数应用程序来解决歧义时:
:foo || some_method(:bar)
then there is no ambiguity needed to be resolved, and the syntax error disappears.
然后没有需要解决的歧义,语法错误消失了。
#2
1
Your :id
is a symbol in Ruby.
你的:id是Ruby中的一个符号。
a = {'id' => 'a', 'value' => 'value'}
a.has_key? 'id'
=> true
a.has_key? :id
=> false
So you need to change your code:
所以你需要改变你的代码:
if params[:id] == '2' or params.has_key? 'id'
abort('params id = 2 or nothing')
end
Note: If you are going to use this code checking for key before checking for value makes more sense.
注意:如果要在检查值之前使用此代码检查密钥更有意义。
Note #2: Tested with :
注2:经测试:
params = {'id' => 'a', 'value' => 'value'}
if params[:id] == '2' or params.has_key? 'id'
abort('params id = 2 or nothing')
end
and Ruby 2.0.0
和Ruby 2.0.0
Also check this question for more information about Ruby Symbols.
另请查看此问题以获取有关Ruby符号的更多信息。
#3
0
It has to do with how Ruby
evaluates that if
statement.
它与Ruby如何评估if语句有关。
Example 1 is like if (params[:id] == '2' || params.has_key?) :id
例1就像if(params [:id] =='2'|| params.has_key?):id
which resolves in an unexpected symbol error as you can see syntax error, unexpected tSYMBEG
.
这可以解决意外的符号错误,因为您可以看到语法错误,意外的tSYMBEG。
#4
0
As guy says... the parser doesn't assume that the symbol is a parameter for the has_key? method
正如家伙所说......解析器不假设该符号是has_key的参数?方法
You can bypass the error by explicitly coding the parentheses
您可以通过显式编写括号来绕过错误
if params[:id] == '2' || params.has_key?(:id)
#1
4
Your problem is happening at:
你的问题发生在:
params[:id] == '2' || params.has_key? :id
which can be simplified to:
可以简化为:
:foo || some_method :bar
which causes the same error. This expression is in principle, ambiguous between
这会导致同样的错误。这个表达原则上是模糊的
(:foo || some_method) :bar (1)
and
和
:foo || (some_method :bar) (2)
When an expression is ambiguous, it is resolved by other factors. One factor, operator precedence tells nothing here about disambiguating between (1) and (2). The next factor is linear order. Since ||
appears before argument application ()
(omitted) in the expression in question, the former applies before the latter. Therefore, the expression is interpreted as (1). Since (:foo || some_method)
would then be parsed as an expression, there would be two expressions next to each other. That is ungrammatical, just as:
当表达式不明确时,它会被其他因素解决。一个因素,运算符优先级在这里没有说明消除(1)和(2)之间的歧义。下一个因素是线性顺序。自||出现在有问题的表达式中的参数application()(省略)之前,前者适用于后者。因此,表达式被解释为(1)。由于(:foo || some_method)将被解析为表达式,因此会有两个表达式彼此相邻。这是不合语法的,就像:
:baz :bar
is ungrammatical.
是不合语法的。
In fact, if you switch the order as:
实际上,如果您将订单切换为:
some_method :bar || :foo
then, it will be interpreted as
然后,它将被解释为
(some_method :bar) || :foo
for the same reason, and syntax error will disappear.
出于同样的原因,语法错误将消失。
Also when you resolve ambiguity by explicitly using parentheses to indicate argument application:
此外,当您通过显式使用括号来指示参数应用程序来解决歧义时:
:foo || some_method(:bar)
then there is no ambiguity needed to be resolved, and the syntax error disappears.
然后没有需要解决的歧义,语法错误消失了。
#2
1
Your :id
is a symbol in Ruby.
你的:id是Ruby中的一个符号。
a = {'id' => 'a', 'value' => 'value'}
a.has_key? 'id'
=> true
a.has_key? :id
=> false
So you need to change your code:
所以你需要改变你的代码:
if params[:id] == '2' or params.has_key? 'id'
abort('params id = 2 or nothing')
end
Note: If you are going to use this code checking for key before checking for value makes more sense.
注意:如果要在检查值之前使用此代码检查密钥更有意义。
Note #2: Tested with :
注2:经测试:
params = {'id' => 'a', 'value' => 'value'}
if params[:id] == '2' or params.has_key? 'id'
abort('params id = 2 or nothing')
end
and Ruby 2.0.0
和Ruby 2.0.0
Also check this question for more information about Ruby Symbols.
另请查看此问题以获取有关Ruby符号的更多信息。
#3
0
It has to do with how Ruby
evaluates that if
statement.
它与Ruby如何评估if语句有关。
Example 1 is like if (params[:id] == '2' || params.has_key?) :id
例1就像if(params [:id] =='2'|| params.has_key?):id
which resolves in an unexpected symbol error as you can see syntax error, unexpected tSYMBEG
.
这可以解决意外的符号错误,因为您可以看到语法错误,意外的tSYMBEG。
#4
0
As guy says... the parser doesn't assume that the symbol is a parameter for the has_key? method
正如家伙所说......解析器不假设该符号是has_key的参数?方法
You can bypass the error by explicitly coding the parentheses
您可以通过显式编写括号来绕过错误
if params[:id] == '2' || params.has_key?(:id)