short cut for gsub("word","")

A

Aryk Grosz

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.
 
T

Tony Arcieri

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

Im guessing something like this already exists.

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

Brian Candler

Aryk said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top