tags:

views:

54

answers:

0

Hi,

I want to monkey patch or extend enumerable. I want to handle nil cases as well and I have come up with the following test case and extensions:

module Enumerable
  def has_elements
    (self) && (self.size > 0)
  end
end

class NilClass
  def has_elements
    false
  end
end

class EnumerableExtensionsTest < ActiveSupport::TestCase

  should "return false for nil" do
    d = nil

    assert_equal(false, d.has_elements)
  end

end

The test passes but this feels wrong and I was wondering if there is a better way of returning false for a call to has_elements, or how should I handle this?

Cheers

Paul