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

前端 未结 18 1782
臣服心动
臣服心动 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:31

    ok, after 5 years have passed....

    if discount.try :nonzero?
      ...
    end
    

    It's important to note that try is defined in the ActiveSupport gem, so it is not available in plain ruby.

提交回复
热议问题