The Case of the False Blank
Stu Halloway tweeted about the following Pastie earlier today:
He questioned the counterintuitive result of blank? returning false on an empty Java array. He then asked which whether Ruby, JRuby or Rails were the culprit.
I played with the Rails console a little bit and started to dig into the code. It turns out that Rails adds a blank? method to the Ruby Object class. Here is it's implementation:
It also aliases blank? to empty? on arrays further down. As a result, the expected behavior of calling blank? on an empty array is to return true:
The Java array does not respond to the empty? method:
We can see that the Java native array does not share the same ancestry as the Ruby Array:
None of the classes and modules returned by x.class.ancestors implement the empty? method. Rails has no choice to fall back to the default implementation and to return false to blank? since any instantiated object isn't nil by definition (except for nil, which is always nil).
The real culprit here isn't Ruby as Stu alluded in a later tweet. Rather, it is the lack of an empty? implementation on the Java native array class ancestry that is the root cause of the surprising behavior. The best fix would be to implement the empty? method on the ArrayJavaProxy class to Rails could call it.