Out of my Mind...

Random Thoughts From My Sleep Deprived Mind.

    • Edit
    • Delete
    • Autopost

    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.

    • 29 December 2009
    • Views
    • 0 Comments
    • Permalink
    • Favorited 0 Times
    • Tweet

    Comments 0 Comments

    Leave a Comment

  • Frederic Jean's Posterous

    New father of twins, still catching up on sleep deprivation.

    Aspiring software craftsman with a deep interest in using alternative language on the Java Virtual Machine to simplify and speed up software development.

  • About Frederic Jean

    New father of twins, still catching up on sleep deprivation.

    Aspiring software craftsman with a deep interest in using alternative language on the Java Virtual Machine to simplify and speed up software development.

  • Subscribe

    Subscribe to this posterous
    Unsubscribe
    Follow this posterous RSS
  • Follow Me

Theme created for Posterous by Obox