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

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)

Some mods.

  • 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    # 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 
    171188  if RUBY_VERSION < '1.9' 
    172189    def test_each_char_with_utf8_string_when_kcode_is_utf8 
    173190      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          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 
     24end