Ticket #11123: add_squish_method_to_string_2.diff
| File add_squish_method_to_string_2.diff, 2.9 kB (added by Henrik N, 7 months ago) |
|---|
-
activesupport/test/core_ext/string_ext_test.rb
old new 168 168 assert !s.end_with?('el') 169 169 end 170 170 171 def test_string_squish 172 original = %{ A string with tabs(\t\t), newlines(\n\n), and 173 many spaces( ). } 174 175 expected = "A string with tabs( ), newlines( ), and many spaces( )." 176 177 # Make sure squish returns what we expect: 178 assert_equal original.squish, expected 179 # But doesn't modify the original string: 180 # assert_not_equal original, expected 181 182 # Make sure squish! returns what we expect: 183 assert_equal original.squish!, expected 184 # And changes the original string: 185 assert_equal original, expected 186 end 187 171 188 if RUBY_VERSION < '1.9' 172 189 def test_each_char_with_utf8_string_when_kcode_is_utf8 173 190 old_kcode, $KCODE = $KCODE, 'UTF8' -
activesupport/lib/active_support/core_ext/string.rb
old new 5 5 require 'active_support/core_ext/string/iterators' unless 'test'.respond_to?(:each_char) 6 6 require 'active_support/core_ext/string/unicode' 7 7 require 'active_support/core_ext/string/xchar' 8 require 'active_support/core_ext/string/filters' 8 9 9 10 class String #:nodoc: 10 11 include ActiveSupport::CoreExtensions::String::Access 11 12 include ActiveSupport::CoreExtensions::String::Conversions 13 include ActiveSupport::CoreExtensions::String::Filters 12 14 include ActiveSupport::CoreExtensions::String::Inflections 13 15 if RUBY_VERSION < '1.9' 14 16 include ActiveSupport::CoreExtensions::String::StartsEndsWith -
activesupport/lib/active_support/core_ext/string/filters.rb
old new 1 module ActiveSupport #:nodoc: 2 module CoreExtensions #:nodoc: 3 module String #:nodoc: 4 module Filters 5 # Returns the string, first removing all whitespace on both ends of 6 # the string, and then changing remaining consecutive whitespace 7 # groups into one space each. 8 # 9 # Examples: 10 # %{ Multi-line 11 # string }.squish # => "Multi-line string" 12 # " foo bar \n \t boo".squish # => "foo bar boo" 13 def squish 14 strip.gsub(/\s+/, ' ') 15 end 16 17 # Performs a destructive squish. See String#squish. 18 def squish! 19 replace(squish) 20 end 21 end 22 end 23 end 24 end