Akamaru Akamaru - 1 year ago 68
Ruby Question

gsub! clean string, taking away apostrophe

Trying to remove the apostrophe within a string. Using the code below I've removed the apostrophe and the spaces, can anyone help me fix it so I'm just removing the apostrophe?

def clean_uniname(text)
return text.gsub!(/\s|"|'/, '')

Answer Source

\s means "whitespace character", and " represents a quote. If you don't want those in your regular expression, pull them. For debugging help try Rubular for testing.

This means your code should be:

def clean_uniname(text)
  text.gsub!(/\s|"|'/, '')


It's worth noting that gsub! returns nil if no changes have been made so an explicit return of text is required here if you're using the return value.