Best ruby idiom for “nil or zero”

前端 未结 21 2154
日久生厌
日久生厌 2020-12-13 03:04

I am looking for a concise way to check a value to see if it is nil or zero. Currently I am doing something like:

if (!val || val == 0)
  # Is nil or zero
e         


        
21条回答
  •  清歌不尽
    2020-12-13 03:45

    Rails does this via attribute query methods, where in addition to false and nil, 0 and "" also evaluate to false.

    if (model.attribute?) # => false if attribute is 0 and model is an ActiveRecord::Base derivation
    

    However it has its share of detractors. http://www.joegrossberg.com/archives/002995.html

提交回复
热议问题