diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 8221dc4abe4509d47215e09f1a617cfe26d4e482..677ec1141ddfc29f21a264e3da6602bb2e79ce89 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/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 diff --git a/activesupport/test/core_ext/blank_test.rb b/activesupport/test/core_ext/blank_test.rb index 97c6b213ba2046e5c168a2d6fc400f3a67f73bcc..98ce8541a311cfd8c4fa64b152b66bcb5c76d6e8 100644 --- a/activesupport/test/core_ext/blank_test.rb +++ b/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