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

后端 未结 14 1057
被撕碎了的回忆
被撕碎了的回忆 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 08:04

    Don't forget any? which is generally !empty?. In Rails I typically check for the presence of something at the end of a statement with if something or unless something then use blank? where needed since it seems to work everywhere.

    0 讨论(0)
  • 2020-11-22 08:06

    A special case is when trying to assess if a boolean value is nil:

    false.present? == false
    false.blank? == true
    false.nil? == false
    

    In this case the recommendation would be to use .nil?

    0 讨论(0)
提交回复
热议问题