Removing quotes from a string

Ruby:
Removing quotes from a string

How to:

Ruby’s got some neat tricks up its sleeve for snipping out those pesky quotation marks. You can use gsub or delete methods to get the job done. Here’s some code to chew on:

# Using gsub to remove double and single quotes
quoted_string = "\"Say 'hello' to my little friend!\""
unquoted_string = quoted_string.gsub(/'|"/, '')
puts unquoted_string 
# Output: Say hello to my little friend!

# If you know you'll only deal with one type of quote
single_quoted_string = "'Stay a while and listen!'"
clean_string = single_quoted_string.delete("'")
puts clean_string 
# Output: Stay a while and listen!

Deep Dive

The history of quotes wraps back to the earliest days of programming, where they often served as string delimiters. Nowadays, as then, you might find yourself needing to remove these quote characters when they’re not needed or when they could interfere with data storage and manipulation.

We’ve talked about gsub and delete but there are other methods too, like tr or tr_s, which give you a little more control or can handle some different use cases:

# tr can also remove quotes
double_quoted_string = "\"Do or do not, there is no try.\""
clean_string = double_quoted_string.tr('\"', '')
puts clean_string 
# Output: Do or do not, there is no try.

Do remember, each of these methods has its use-cases. gsub is more powerful when you’re dealing with complex patterns or multiple substitutions. delete and tr work beautifully for simple, straightforward character removals.

See Also

For additional reading, and to see these methods in action within larger codebases, check out: