How to understand nil vs. empty vs. blank in Ruby

后端 未结 14 1270
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 07:28

I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails. Here\'s the

14条回答
  •  爱一瞬间的悲伤
    2020-11-22 07:44

    Rails 4

    an alternative to @corban-brook 's 'Array gotcha: blank?' for checking if an arrays only holds empty values and can be regarded as blank? true:

    [ nil, '' ].all? &:blank? == true
    

    one could also do:

    [nil, '', "", " ",' '].reject(&:blank?).blank? == true
    

提交回复
热议问题