Make String#remove
and String#remove!
accept multiple arguments
This commit is contained in:
parent
016af48b86
commit
73ad151030
@ -1,3 +1,7 @@
|
|||||||
|
* `String#remove` and `String#remove!` accept multiple arguments.
|
||||||
|
|
||||||
|
*Pavel Pravosud*
|
||||||
|
|
||||||
* Corrected Inflector#underscore handling of multiple successive acroynms.
|
* Corrected Inflector#underscore handling of multiple successive acroynms.
|
||||||
|
|
||||||
*James Le Cuirot*
|
*James Le Cuirot*
|
||||||
|
@ -20,14 +20,18 @@ def squish!
|
|||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a new string with all occurrences of the pattern removed. Short-hand for String#gsub(pattern, '').
|
# Returns a new string with all occurrences of the patterns removed.
|
||||||
def remove(pattern)
|
def remove(*patterns)
|
||||||
gsub pattern, ''
|
dup.remove!(*patterns)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Alters the string by removing all occurrences of the pattern. Short-hand for String#gsub!(pattern, '').
|
# Alters the string by removing all occurrences of the patterns.
|
||||||
def remove!(pattern)
|
def remove!(*patterns)
|
||||||
gsub! pattern, ''
|
patterns.each do |pattern|
|
||||||
|
gsub! pattern, ""
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
|
# 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
|
end
|
||||||
|
|
||||||
def test_remove
|
def test_remove
|
||||||
assert_equal "Summer", "Fast Summer".remove(/Fast /)
|
original = "This is a good day to die"
|
||||||
assert_equal "Summer", "Fast Summer".remove!(/Fast /)
|
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
|
end
|
||||||
|
|
||||||
def test_constantize
|
def test_constantize
|
||||||
|
Loading…
Reference in New Issue
Block a user