aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2008-02-16 00:02:30 +0000
committerMichael Koziarski <michael@koziarski.com>2008-02-16 00:02:30 +0000
commit4ff26f51f61f9bf7d0bb34557e72c1280ee4f859 (patch)
tree9ce2f97947ecc132937a26c517df4594379a9179 /activesupport/lib/active_support/core_ext
parent191ffc94568ca22885eac7001cae7fef452dcfdd (diff)
downloadrails-4ff26f51f61f9bf7d0bb34557e72c1280ee4f859.tar.gz
rails-4ff26f51f61f9bf7d0bb34557e72c1280ee4f859.tar.bz2
rails-4ff26f51f61f9bf7d0bb34557e72c1280ee4f859.zip
Add String#squish and String#squish! to remove consecutive chunks of whitespace. Closes #11123 [jordi, Henrik N]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8878 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/string.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/string/filters.rb24
2 files changed, 26 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb
index 5497b6f6f6..a83474f278 100644
--- a/activesupport/lib/active_support/core_ext/string.rb
+++ b/activesupport/lib/active_support/core_ext/string.rb
@@ -5,10 +5,12 @@ require 'active_support/core_ext/string/starts_ends_with'
require 'active_support/core_ext/string/iterators' unless 'test'.respond_to?(:each_char)
require 'active_support/core_ext/string/unicode'
require 'active_support/core_ext/string/xchar'
+require 'active_support/core_ext/string/filters'
class String #:nodoc:
include ActiveSupport::CoreExtensions::String::Access
include ActiveSupport::CoreExtensions::String::Conversions
+ include ActiveSupport::CoreExtensions::String::Filters
include ActiveSupport::CoreExtensions::String::Inflections
if RUBY_VERSION < '1.9'
include ActiveSupport::CoreExtensions::String::StartsEndsWith
diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb
new file mode 100644
index 0000000000..3a69131ea0
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/string/filters.rb
@@ -0,0 +1,24 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module String #:nodoc:
+ module Filters
+ # Returns the string, first removing all whitespace on both ends of
+ # the string, and then changing remaining consecutive whitespace
+ # groups into one space each.
+ #
+ # Examples:
+ # %{ Multi-line
+ # string }.squish # => "Multi-line string"
+ # " foo bar \n \t boo".squish # => "foo bar boo"
+ def squish
+ strip.gsub(/\s+/, ' ')
+ end
+
+ # Performs a destructive squish. See String#squish.
+ def squish!
+ replace(squish)
+ end
+ end
+ end
+ end
+end