public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
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
NZKoz (author)
Fri Feb 15 16:02:30 -0800 2008
commit  4ff26f51f61f9bf7d0bb34557e72c1280ee4f859
tree    9ce2f97947ecc132937a26c517df4594379a9179
parent  191ffc94568ca22885eac7001cae7fef452dcfdd
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Add String#squish and String#squish! to remove consecutive chunks of whitespace. #11123 [jordi, Henrik N]
0
+
0
 * Serialize BigDecimals as Floats when using to_yaml. #8746 [ernesto.jimenez]
0
 
0
 * Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time [Geoff Buesing]
...
5
6
7
 
8
9
10
11
 
12
13
14
...
5
6
7
8
9
10
11
12
13
14
15
16
0
@@ -5,10 +5,12 @@ require 'active_support/core_ext/string/starts_ends_with'
0
 require 'active_support/core_ext/string/iterators' unless 'test'.respond_to?(:each_char)
0
 require 'active_support/core_ext/string/unicode'
0
 require 'active_support/core_ext/string/xchar'
0
+require 'active_support/core_ext/string/filters'
0
 
0
 class String #:nodoc:
0
   include ActiveSupport::CoreExtensions::String::Access
0
   include ActiveSupport::CoreExtensions::String::Conversions
0
+ include ActiveSupport::CoreExtensions::String::Filters
0
   include ActiveSupport::CoreExtensions::String::Inflections
0
   if RUBY_VERSION < '1.9'
0
     include ActiveSupport::CoreExtensions::String::StartsEndsWith
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -0,0 +1,24 @@
0
+module ActiveSupport #:nodoc:
0
+ module CoreExtensions #:nodoc:
0
+ module String #:nodoc:
0
+ module Filters
0
+ # Returns the string, first removing all whitespace on both ends of
0
+ # the string, and then changing remaining consecutive whitespace
0
+ # groups into one space each.
0
+ #
0
+ # Examples:
0
+ # %{ Multi-line
0
+ # string }.squish # => "Multi-line string"
0
+ # " foo bar \n \t boo".squish # => "foo bar boo"
0
+ def squish
0
+ strip.gsub(/\s+/, ' ')
0
+ end
0
+
0
+ # Performs a destructive squish. See String#squish.
0
+ def squish!
0
+ replace(squish)
0
+ end
0
+ end
0
+ end
0
+ end
0
+end
...
168
169
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
172
173
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
0
@@ -168,6 +168,23 @@ class StringInflectionsTest < Test::Unit::TestCase
0
     assert !s.end_with?('el')
0
   end
0
 
0
+ def test_string_squish
0
+ original = %{ A string with tabs(\t\t), newlines(\n\n), and
0
+ many spaces( ). }
0
+
0
+ expected = "A string with tabs( ), newlines( ), and many spaces( )."
0
+
0
+ # Make sure squish returns what we expect:
0
+ assert_equal original.squish, expected
0
+ # But doesn't modify the original string:
0
+ assert_not_equal original, expected
0
+
0
+ # Make sure squish! returns what we expect:
0
+ assert_equal original.squish!, expected
0
+ # And changes the original string:
0
+ assert_equal original, expected
0
+ end
0
+
0
   if RUBY_VERSION < '1.9'
0
     def test_each_char_with_utf8_string_when_kcode_is_utf8
0
       old_kcode, $KCODE = $KCODE, 'UTF8'

Comments

    No one has commented yet.