Optionality in Ruby on Rails: A blankness idiosyncrasy
A colleague and I discovered a funny little oddity in the way
ActiveSupport
implements #blank?
for different classes.
Source code here:
Discussion below:
The way String#blank?
is defined has an extra convenience in that it
recognizes strings with only whitespace characters as “blank”.
There’s no corresponding convenience defined for, say Array#blank?
and it’s interesting to think about why.
Imagine what it would take to consider this blank:
[nil]
or this:
[nil, nil]
or this:
[[], nil, [nil]]
You’d need something that would recursively test for blankness every member of the array. Strings are non-recursive, and only contain characters, so the convenience can be much more efficiently implemented, and had basically no special cases.
And that’s basically it.