diff options
Diffstat (limited to 'activesupport/lib/active_support/core_ext/object')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/blank.rb | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 039c50a4a2..b08321aeeb 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -97,8 +97,6 @@ class Hash end class String - BLANK_RE = /\A[[:space:]]*\z/ - # A string is blank if it's empty or contains whitespaces only: # # ''.blank? # => true @@ -112,7 +110,13 @@ class String # # @return [true, false] def blank? - BLANK_RE === self + # In practice, the majority of blank strings are empty. The predicate is + # about 3.5x faster than the regexp check so we first test empty?, and then + # fallback. Penalty for the rest of strings is marginal. + # + # Double negation in the second operand is also a performance tweak, it is + # faster than the positive \A[[:space:]]*\z. + empty? || !(/[[:^space:]]/ === self) end end |