# Short cut for gsub("word","")

**URL:** <https://www.ruby-forum.com/t/short-cut-for-gsub-word/165437>\
**Category:** Ruby\
**Created:** [April 14, 2009, 12:20am UTC](https://www.ruby-forum.com/t/short-cut-for-gsub-word/165437 "2009-04-14T00:20:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 14, 2009, 12:20am UTC](https://www.ruby-forum.com/t/short-cut-for-gsub-word/165437/1 "2009-04-14T00:20:01Z")

</div>

is there any shortcut for removing a string of characters from text,  
like gsub(“word”,""). I looked into String#delete but it will delete all  
the characters in the entire word.

Im guessing something like this already exists.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 14, 2009, 1:41am UTC](https://www.ruby-forum.com/t/short-cut-for-gsub-word/165437/2 "2009-04-14T01:41:58Z")

</div>

On Mon, Apr 13, 2009 at 4:20 PM, Aryk G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Im guessing something like this already exists.

I’m kind of surprised String#- doesn’t work (it isn’t defined)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 15, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/short-cut-for-gsub-word/165437/3 "2009-04-15T15:15:18Z")

</div>

Aryk G. wrote:

> is there any shortcut for removing a string of characters from text,  
> like gsub(“word”,""). I looked into String#delete but it will delete all  
> the characters in the entire word.

str[“word”] = “”  
str[/word/] = “”

but these only do the first instance.

If you find yourself writing

str.gsub! /word/, ‘’

repeatedly, to the point where it is tiresome, then factor it out  
yourself, depending on exactly what your needs are. e.g.

class String  
def remove!(str)  
gsub!(str, ‘’)  
end  
def remove(str)  
res = dup  
res.remove!(str)  
res  
end  
end
