Checking if a variable is not nil and not zero in ruby

前端 未结 18 1752
臣服心动
臣服心动 2020-12-22 15:38

I am using the following code to check if a variable is not nil and not zero

if(discount != nil && discount != 0) 
  ...
end

Is the

18条回答
  •  眼角桃花
    2020-12-22 16:35

    def is_nil_and_zero(data)
         data.blank? || data == 0 
    end  
    

    If we pass "" it will return false whereas blank? returns true. Same is the case when data = false blank? returns true for nil, false, empty, or a whitespace string. So it's better to use blank? method to avoid empty string as well.

提交回复
热议问题