mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
commit
60c45b0d0a
2 changed files with 15 additions and 0 deletions
|
@ -13,6 +13,9 @@ class String
|
|||
end
|
||||
|
||||
# Performs a destructive squish. See String#squish.
|
||||
# str = " foo bar \n \t boo"
|
||||
# str.squish! # => "foo bar boo"
|
||||
# str # => "foo bar boo"
|
||||
def squish!
|
||||
gsub!(/\A[[:space:]]+/, '')
|
||||
gsub!(/[[:space:]]+\z/, '')
|
||||
|
@ -21,11 +24,17 @@ class String
|
|||
end
|
||||
|
||||
# Returns a new string with all occurrences of the patterns removed.
|
||||
# str = "foo bar test"
|
||||
# str.remove(" test") # => "foo bar"
|
||||
# str # => "foo bar test"
|
||||
def remove(*patterns)
|
||||
dup.remove!(*patterns)
|
||||
end
|
||||
|
||||
# Alters the string by removing all occurrences of the patterns.
|
||||
# str = "foo bar test"
|
||||
# str.remove!(" test") # => "foo bar"
|
||||
# str # => "foo bar"
|
||||
def remove!(*patterns)
|
||||
patterns.each do |pattern|
|
||||
gsub! pattern, ""
|
||||
|
|
|
@ -266,6 +266,12 @@ class StringInflectionsTest < ActiveSupport::TestCase
|
|||
assert_equal "This is a good day to die", original
|
||||
end
|
||||
|
||||
def test_remove_for_multiple_occurrences
|
||||
original = "This is a good day to die to die"
|
||||
assert_equal "This is a good day", original.remove(" to die")
|
||||
assert_equal "This is a good day to die 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")
|
||||
|
|
Loading…
Reference in a new issue