Why does false.blank? == true?

I’m quite sure blank? and friends are universally loved. But, just recently I’ve learned through an annoying bug :sweat_smile: that false.blank? == true. This seems unexpected. Can anyone help me to understand the rationale for that?

1 Like

The great question is: why is there a method that verifies if a value is blank, if that value could never, by definition, be blank? :laughing:

Accordingly to the 7.0.4 docs: “An instance of ActiveSupport::TimeWithZone is never blank”

Of course that blank? has at least another 3 different implementations in other classes, but in this case doesn’t make sense to me either.

I’ll keep digging into docs and implementation trying to answer that, but it oddingly seems a bug. Nice observation!

false.blank? == true seems correct for me.

In Ruby only false and nil evaluate false in a conditional. Everything else evaluates to true. These semantics are simple but sometimes you want different semantics. You want things like an empty string or an empty array to evaluate to false. Therefore as an extension Rails adds the concept of “blank” to includes other things. But the false and nil are also considered blank. The new items added are in addition to what is already considered false. Some languages/libraries uses the term “falsey” as this sort of extended version of false.

value Ruby Rails
nil false blank
false false blank
[] true blank
'' true blank

NOTE: There are other things that can evaluate to blank besides just [] and ''. Really anything that responds to empty? is blank?. I just used [] and '' as to common examples of things that are blank.

1 Like