I know some ways to check if parameter is not nil
我知道一些检查参数是否为零的方法
if param[:some_value]
if param[:some_value].present?
if !param[:some_value].nil? #unless param[:some_value].nil?
if !param[:some_value].blank? #unless param[:some_value].blank?
Which one is correct and most popular? What is the difference between them? I'd rather use if param[:some_value]
because it is simplest and shorterst.
哪一个是正确的,最受欢迎的?它们之间有什么区别?我宁愿使用if param [:some_value],因为它最简单,最短。
4 个解决方案
#1
15
Here are some differences between nil?
, blank?
and present?
:
以下是nil?之间的一些区别,空白?和礼物?:
>> "".nil?
=> false
>> "".blank?
=> true
>> "".present?
=> false
>> " ".nil?
=> false
>> " ".blank?
=> true
>> " ".present?
=> false
Note that present?
translates to not nil and not blank
. Also note that while nil?
is provided by Ruby, blank?
and present?
are helpers provided by Rails.
请注意礼物?转换为不是零而不是空白。还要注意,虽然没有?由Ruby提供,空白?和礼物?是Rails提供的帮手。
So, which one to choose? It depends on what you want, of course, but when evaluating params[:some_value], you will usually want to check not only that it is not nil, but also if it is an empty string. Both of these are covered by present?
.
那么,选择哪一个?当然,这取决于你想要什么,但是在评估params [:some_value]时,你通常不仅要检查它不是nil,还要检查它是否为空字符串。这两个都被现在覆盖?
#2
3
The relevant cases are:
相关案例如下:
- The hash lacks the key (and its value)
- The hash has the value
nil
for the key - The hash has the value empty string (
""
) or the empty array ([]
) for the key - The hash has some other value for the key
哈希缺少密钥(及其值)
散列的值为nil
散列的值为空字符串(“”)或空数组([])
哈希值具有密钥的其他值
Depending on where you want to draw the line, use the appropriate method.
根据您想要绘制线的位置,使用适当的方法。
- 1 vs. 2, 3, 4:
!param.key?(:some_value)
- 1, 2 vs. 3, 4:
param[:some_value].nil?
- 1, 2, 3 vs. 4:
param[:some_value].blank?
- 2, 3, 4 vs. 1:
param.key?(:some_value)
- 3, 4 vs. 1, 2:
param[:some_value]
- 4 vs. 1, 2, 3:
param[:some_value].present?
1对2,3,4:!param.key?(:some_value)
1,2对3,4:param [:some_value] .nil?
1,2,3 vs. 4:param [:some_value] .blank?
2,3,4 vs. 1:param.key?(:some_value)
3,4比1,2:param [:some_value]
4对1,2,3:param [:some_value] .present?
#3
2
if param[:some_value]
is the most common one.
如果param [:some_value]是最常见的。
But if you're working with boolean data (true
,false
) the best method would be if !param[:some_value].nil?
, because:
但是如果你使用布尔数据(true,false),最好的方法是if!param [:some_value] .nil ?,因为:
>> hash = {foo:false, bar:nil}
>> !! hash[:foo]
=> false
>> !! hash[:bar]
=> false
>> !! !hash[:foo].nil?
=> true
>> !! !hash[:bar].nil?
=> false
(!!
returns the boolean value, which would be tested in an if-statement)
(!!返回布尔值,将在if语句中测试)
Also it could be important to test if the value is really nil, or if the key-value combo isn't defined. key?(key)
does that for you.
此外,测试值是否为零或者未定义键值组合可能很重要。键(键)为你做的。
>> hash ={foo:nil}
>> hash[:foo]
=> nil
>> hash[:not_there]
=> nil
>> hash.key?(:foo)
=> true
>> hash.key?(:not_there)
=> false
#4
1
It depends what you want to do, but I generally use these forms:
这取决于你想做什么,但我通常使用这些形式:
if param[:some_value]
...
end
# and
param[:some_value] && something
# or
param[:some_value] || somethinn
Of course all of them only work if the parameter can't be false
. In that case I'd go for the unless param[:some_value].nil?
version.
当然,只有参数不能为假时,它们才有效。在那种情况下,我会去除非参数[:some_value] .nil?版。
#1
15
Here are some differences between nil?
, blank?
and present?
:
以下是nil?之间的一些区别,空白?和礼物?:
>> "".nil?
=> false
>> "".blank?
=> true
>> "".present?
=> false
>> " ".nil?
=> false
>> " ".blank?
=> true
>> " ".present?
=> false
Note that present?
translates to not nil and not blank
. Also note that while nil?
is provided by Ruby, blank?
and present?
are helpers provided by Rails.
请注意礼物?转换为不是零而不是空白。还要注意,虽然没有?由Ruby提供,空白?和礼物?是Rails提供的帮手。
So, which one to choose? It depends on what you want, of course, but when evaluating params[:some_value], you will usually want to check not only that it is not nil, but also if it is an empty string. Both of these are covered by present?
.
那么,选择哪一个?当然,这取决于你想要什么,但是在评估params [:some_value]时,你通常不仅要检查它不是nil,还要检查它是否为空字符串。这两个都被现在覆盖?
#2
3
The relevant cases are:
相关案例如下:
- The hash lacks the key (and its value)
- The hash has the value
nil
for the key - The hash has the value empty string (
""
) or the empty array ([]
) for the key - The hash has some other value for the key
哈希缺少密钥(及其值)
散列的值为nil
散列的值为空字符串(“”)或空数组([])
哈希值具有密钥的其他值
Depending on where you want to draw the line, use the appropriate method.
根据您想要绘制线的位置,使用适当的方法。
- 1 vs. 2, 3, 4:
!param.key?(:some_value)
- 1, 2 vs. 3, 4:
param[:some_value].nil?
- 1, 2, 3 vs. 4:
param[:some_value].blank?
- 2, 3, 4 vs. 1:
param.key?(:some_value)
- 3, 4 vs. 1, 2:
param[:some_value]
- 4 vs. 1, 2, 3:
param[:some_value].present?
1对2,3,4:!param.key?(:some_value)
1,2对3,4:param [:some_value] .nil?
1,2,3 vs. 4:param [:some_value] .blank?
2,3,4 vs. 1:param.key?(:some_value)
3,4比1,2:param [:some_value]
4对1,2,3:param [:some_value] .present?
#3
2
if param[:some_value]
is the most common one.
如果param [:some_value]是最常见的。
But if you're working with boolean data (true
,false
) the best method would be if !param[:some_value].nil?
, because:
但是如果你使用布尔数据(true,false),最好的方法是if!param [:some_value] .nil ?,因为:
>> hash = {foo:false, bar:nil}
>> !! hash[:foo]
=> false
>> !! hash[:bar]
=> false
>> !! !hash[:foo].nil?
=> true
>> !! !hash[:bar].nil?
=> false
(!!
returns the boolean value, which would be tested in an if-statement)
(!!返回布尔值,将在if语句中测试)
Also it could be important to test if the value is really nil, or if the key-value combo isn't defined. key?(key)
does that for you.
此外,测试值是否为零或者未定义键值组合可能很重要。键(键)为你做的。
>> hash ={foo:nil}
>> hash[:foo]
=> nil
>> hash[:not_there]
=> nil
>> hash.key?(:foo)
=> true
>> hash.key?(:not_there)
=> false
#4
1
It depends what you want to do, but I generally use these forms:
这取决于你想做什么,但我通常使用这些形式:
if param[:some_value]
...
end
# and
param[:some_value] && something
# or
param[:some_value] || somethinn
Of course all of them only work if the parameter can't be false
. In that case I'd go for the unless param[:some_value].nil?
version.
当然,只有参数不能为假时,它们才有效。在那种情况下,我会去除非参数[:some_value] .nil?版。