Make String#remove and String#remove! accept multiple arguments

This commit is contained in:
Pavel Pravosud 2014-10-24 13:25:55 -04:00
parent 016af48b86
commit 73ad151030
3 changed files with 26 additions and 8 deletions

@ -1,3 +1,7 @@
* `String#remove` and `String#remove!` accept multiple arguments.
*Pavel Pravosud*
* Corrected Inflector#underscore handling of multiple successive acroynms.
*James Le Cuirot*

@ -20,14 +20,18 @@ def squish!
self
end
# Returns a new string with all occurrences of the pattern removed. Short-hand for String#gsub(pattern, '').
def remove(pattern)
gsub pattern, ''
# Returns a new string with all occurrences of the patterns removed.
def remove(*patterns)
dup.remove!(*patterns)
end
# Alters the string by removing all occurrences of the pattern. Short-hand for String#gsub!(pattern, '').
def remove!(pattern)
gsub! pattern, ''
# Alters the string by removing all occurrences of the patterns.
def remove!(*patterns)
patterns.each do |pattern|
gsub! pattern, ""
end
self
end
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:

@ -260,8 +260,18 @@ def test_truncate_should_not_be_html_safe
end
def test_remove
assert_equal "Summer", "Fast Summer".remove(/Fast /)
assert_equal "Summer", "Fast Summer".remove!(/Fast /)
original = "This is a good day to die"
assert_equal "This is a good day", original.remove(" to die")
assert_equal "This is a good day", original.remove(" to ", /die/)
assert_equal "This is a good day to die", original
end
def test_remove!
original = "This is a very good day to die"
assert_equal "This is a good day to die", original.remove!(" very")
assert_equal "This is a good day to die", original
assert_equal "This is a good day", original.remove!(" to ", /die/)
assert_equal "This is a good day", original
end
def test_constantize