Skip to content

Commit 5da23a3

Browse files
author
David Heinemeier Hansson
committed
Add String#remove(pattern) as a short-hand for the common pattern of String#gsub(pattern, '')
1 parent d126a08 commit 5da23a3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add String#remove(pattern) as a short-hand for the common pattern of String#gsub(pattern, '')
2+
3+
*DHH*
4+
15
* Adds a new deprecation behaviour that raises an exception. Throwing this
26
line into +config/environments/development.rb+
37

activesupport/lib/active_support/core_ext/string/filters.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def squish!
2020
self
2121
end
2222

23+
# Returns a new string with all occurances of the pattern removed. Short-hand for String#gsub(pattern, '').
24+
def remove(pattern)
25+
gsub pattern, ''
26+
end
27+
28+
# Alters the string by removing all occurances of the pattern. Short-hand for String#gsub!(pattern, '').
29+
def remove!(pattern)
30+
gsub! pattern, ''
31+
end
32+
2333
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
2434
#
2535
# 'Once upon a time in a world far far away'.truncate(27)

activesupport/test/core_ext/string_ext_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ def test_truncate_multibyte
277277
def test_truncate_should_not_be_html_safe
278278
assert !"Hello World!".truncate(12).html_safe?
279279
end
280+
281+
def test_remove
282+
assert_equal "Summer", "Fast Summer".remove(/Fast /)
283+
assert_equal "Summer", "Fast Summer".remove!(/Fast /)
284+
end
280285

281286
def test_constantize
282287
run_constantize_tests_on do |string|

0 commit comments

Comments
 (0)