Ruby function to remove all white spaces? Ruby function to remove all white spaces? ruby ruby

Ruby function to remove all white spaces?


If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead .


s = "I have white space".delete(' ')

And to emulate PHP's trim() function:

s = "   I have leading and trailing white space   ".strip


Related answer:

"   clean up my edges    ".strip

returns

"clean up my edges"