Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11123: add_squish_method_to_string.diff

File add_squish_method_to_string.diff, 2.8 kB (added by jordi, 8 months ago)
  • activesupport/test/core_ext/string_ext_test.rb

    old new  
    168168    assert !s.end_with?('el') 
    169169  end 
    170170 
     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 
     180    # Make sure squish! returns what we expect: 
     181    assert_equal original.squish!, expected 
     182 
     183    # Make sure squish! changes the original to what we expect: 
     184    assert_equal original, expected 
     185  end 
     186 
    171187  if RUBY_VERSION < '1.9' 
    172188    def test_each_char_with_utf8_string_when_kcode_is_utf8 
    173189      old_kcode, $KCODE = $KCODE, 'UTF8' 
  • activesupport/lib/active_support/core_ext/string.rb

    old new  
    55require 'active_support/core_ext/string/iterators' unless 'test'.respond_to?(:each_char) 
    66require 'active_support/core_ext/string/unicode' 
    77require 'active_support/core_ext/string/xchar' 
     8require 'active_support/core_ext/string/filters' 
    89 
    910class String #:nodoc: 
    1011  include ActiveSupport::CoreExtensions::String::Access 
    1112  include ActiveSupport::CoreExtensions::String::Conversions 
     13  include ActiveSupport::CoreExtensions::String::Filters 
    1214  include ActiveSupport::CoreExtensions::String::Inflections 
    1315  if RUBY_VERSION < '1.9' 
    1416    include ActiveSupport::CoreExtensions::String::StartsEndsWith 
  • activesupport/lib/active_support/core_ext/string/filters.rb

    old new  
     1module 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          dup.squish! 
     15        end 
     16 
     17        # Performs a destructive squish. See String.squish. 
     18        def squish! 
     19          strip! 
     20          gsub!(/\s+/, ' ') 
     21          self 
     22        end 
     23      end 
     24    end 
     25  end 
     26end