Permalink
Browse files

Merge pull request #2052 from amatsuda/fullwidth_blank

treat fullwidth whitespace as a blank character
  • Loading branch information...
2 parents 54ab63e + 9c60860 commit 003c6516dcffcd846d1f257a16062cea657cbd33 @tenderlove tenderlove committed Jul 14, 2011
View
6 activesupport/lib/active_support/core_ext/object/blank.rb
@@ -86,14 +86,18 @@ class Hash
end
class String
+ # 0x3000: fullwidth whitespace
+ NON_WHITESPACE_REGEXP = %r![^\s#{[0x3000].pack("U")}]!
+
# A string is blank if it's empty or contains whitespaces only:
#
# "".blank? # => true
# " ".blank? # => true
+ # " ".blank? # => true
# " something here ".blank? # => false
#
def blank?
- self !~ /\S/
+ self !~ NON_WHITESPACE_REGEXP
end
end
View
2 activesupport/test/core_ext/blank_test.rb
@@ -2,7 +2,7 @@
require 'active_support/core_ext/object/blank'
class BlankTest < Test::Unit::TestCase
- BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ]
+ BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', [], {} ]
NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]
def test_blank

0 comments on commit 003c651

Please sign in to comment.