mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
191ffc9456
commit
4ff26f51f6
4 changed files with 45 additions and 0 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Add String#squish and String#squish! to remove consecutive chunks of whitespace. #11123 [jordi, Henrik N]
|
||||
|
||||
* Serialize BigDecimals as Floats when using to_yaml. #8746 [ernesto.jimenez]
|
||||
|
||||
* Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time [Geoff Buesing]
|
||||
|
|
|
@ -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
|
||||
|
|
24
activesupport/lib/active_support/core_ext/string/filters.rb
Normal file
24
activesupport/lib/active_support/core_ext/string/filters.rb
Normal file
|
@ -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
|
|
@ -168,6 +168,23 @@ class StringInflectionsTest < Test::Unit::TestCase
|
|||
assert !s.end_with?('el')
|
||||
end
|
||||
|
||||
def test_string_squish
|
||||
original = %{ A string with tabs(\t\t), newlines(\n\n), and
|
||||
many spaces( ). }
|
||||
|
||||
expected = "A string with tabs( ), newlines( ), and many spaces( )."
|
||||
|
||||
# Make sure squish returns what we expect:
|
||||
assert_equal original.squish, expected
|
||||
# But doesn't modify the original string:
|
||||
assert_not_equal original, expected
|
||||
|
||||
# Make sure squish! returns what we expect:
|
||||
assert_equal original.squish!, expected
|
||||
# And changes the original string:
|
||||
assert_equal original, expected
|
||||
end
|
||||
|
||||
if RUBY_VERSION < '1.9'
|
||||
def test_each_char_with_utf8_string_when_kcode_is_utf8
|
||||
old_kcode, $KCODE = $KCODE, 'UTF8'
|
||||
|
|
Loading…
Reference in a new issue