diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2013-08-13 12:15:48 -0500 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2013-08-13 12:16:25 -0500 |
commit | 5da23a3f921f0a4a3139495d2779ab0d3bd4cb5f (patch) | |
tree | 4a4f6b44fb99f091418126261b684151d258fa48 /activesupport | |
parent | d126a081ed840340dff5c6299d01d42aaaf32608 (diff) | |
download | rails-5da23a3f921f0a4a3139495d2779ab0d3bd4cb5f.tar.gz rails-5da23a3f921f0a4a3139495d2779ab0d3bd4cb5f.tar.bz2 rails-5da23a3f921f0a4a3139495d2779ab0d3bd4cb5f.zip |
Add String#remove(pattern) as a short-hand for the common pattern of String#gsub(pattern, '')
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/filters.rb | 10 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 5 |
3 files changed, 19 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 321e30681b..81c6246c5c 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add String#remove(pattern) as a short-hand for the common pattern of String#gsub(pattern, '') + + *DHH* + * Adds a new deprecation behaviour that raises an exception. Throwing this line into +config/environments/development.rb+ diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index a4cacaf789..2e2dd86a8b 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -20,6 +20,16 @@ class String self end + # Returns a new string with all occurances of the pattern removed. Short-hand for String#gsub(pattern, ''). + def remove(pattern) + gsub pattern, '' + end + + # Alters the string by removing all occurances of the pattern. Short-hand for String#gsub!(pattern, ''). + def remove!(pattern) + gsub! pattern, '' + end + # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>: # # 'Once upon a time in a world far far away'.truncate(27) diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 12126fb9ef..2537af0394 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -277,6 +277,11 @@ class StringInflectionsTest < ActiveSupport::TestCase def test_truncate_should_not_be_html_safe assert !"Hello World!".truncate(12).html_safe? end + + def test_remove + assert_equal "Summer", "Fast Summer".remove(/Fast /) + assert_equal "Summer", "Fast Summer".remove!(/Fast /) + end def test_constantize run_constantize_tests_on do |string| |