[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

short cut for gsub("word",""

Aryk Grosz

4/13/2009 10:20:00 PM

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.
--
Posted via http://www.ruby-....

2 Answers

Tony Arcieri

4/13/2009 11:41:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, Apr 13, 2009 at 4:20 PM, Aryk Grosz <tennisbum2002@hotmail.com>wrote:

> Im guessing something like this already exists.
>

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

--
Tony Arcieri
medioh.com

Brian Candler

4/15/2009 1:15:00 PM

0

Aryk Grosz 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
--
Posted via http://www.ruby-....