Skip to content

Instantly share code, notes, and snippets.

@mvrilo
Created September 11, 2011 17:21
Show Gist options
  • Select an option

  • Save mvrilo/1209845 to your computer and use it in GitHub Desktop.

Select an option

Save mvrilo/1209845 to your computer and use it in GitHub Desktop.
nil or empty
require 'test/unit'
class Object
def blank?
if self.respond_to?(:empty?)
if self.class == String then self.strip.empty? else self.empty? end
else
self.nil?
end
end
end
class BlankTest < Test::Unit::TestCase
def test_simple
assert_equal false, 123.blank?
assert_equal false, 0.blank?
assert_equal false, "Test".blank?
assert_equal true, " ".blank?
assert_equal true, "".blank?
assert_equal true, nil.blank?
assert_equal true, [].blank?
assert_equal true, {}.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment