Best ruby idiom for “nil or zero”

前端 未结 21 2173
日久生厌
日久生厌 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:50

    If you really like method names with question marks at the end:

    
    if val.nil? || val.zero?
      # do stuff
    end
    
    

    Your solution is fine, as are a few of the other solutions.

    Ruby can make you search for a pretty way to do everything, if you're not careful.

提交回复
热议问题