Permalink
Browse files

Add String#remove(pattern) as a short-hand for the common pattern of …

…String#gsub(pattern, '')
  • Loading branch information...
dhh committed Aug 13, 2013
1 parent d126a08 commit 5da23a3f921f0a4a3139495d2779ab0d3bd4cb5f
@@ -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+
@@ -20,6 +20,16 @@ def squish!
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)
@@ -277,6 +277,11 @@ def test_truncate_multibyte
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|

4 comments on commit 5da23a3

@robertomiranda

This comment has been minimized.

Show comment
Hide comment
@robertomiranda

robertomiranda Aug 13, 2013

Contributor

❤️

Contributor

robertomiranda replied Aug 13, 2013

❤️

@mdespuits

This comment has been minimized.

Show comment
Hide comment
@mdespuits

mdespuits Aug 13, 2013

Contributor

I could have sworn there was something built into Ruby that already did this, but I guess not!

❤️

Contributor

mdespuits replied Aug 13, 2013

I could have sworn there was something built into Ruby that already did this, but I guess not!

❤️

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Aug 14, 2013

Member

There is String#delete, which does not take a regex.

Member

steveklabnik replied Aug 14, 2013

There is String#delete, which does not take a regex.

@jonatack

This comment has been minimized.

Show comment
Hide comment
@jonatack

jonatack Jan 5, 2014

Contributor

Good idea.

Contributor

jonatack replied Jan 5, 2014

Good idea.

Please sign in to comment.