Skip to content

Commit

Permalink
treat fullwidth whitespace as a blank character
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Jul 13, 2011
1 parent 704ee0d commit 9c60860
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion activesupport/lib/active_support/core_ext/object/blank.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/core_ext/blank_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 9c60860

Please sign in to comment.