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

后端 未结 14 1063
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 07:45

    Everybody else has explained well what is the difference.

    I would like to add in Ruby On Rails, it is better to use obj.blank? or obj.present? instead of obj.nil? or obj.empty?.

    obj.blank? handles all types nil, '', [], {}, and returns true if values are not available and returns false if values are available on any type of object.

提交回复
热议问题