更新时间:2023-02-08 20:15:21
第二个子句不需要 !variable.nil?
检查——如果评估达到那个点,variable.nil
肯定是错误的(因为短路).
The second clause does not need a !variable.nil?
check—if evaluation reaches that point, variable.nil
is guaranteed to be false (because of short-circuiting).
这应该足够了:
variable = id if variable.nil? || variable.empty?
如果您使用 Ruby on Rails,Object.blank?
解决了这个问题:
If you're working with Ruby on Rails, Object.blank?
solves this exact problem:
如果对象为 false、空或空白字符串,则该对象为空.例如,""
、" "
、nil
、[]
和 {}代码>都是空白的.
An object is blank if it’s false, empty, or a whitespace string. For example,
""
," "
,nil
,[]
, and{}
are all blank.