Use a regular expression to match repeating whitespace (\s+) and replace it by a space.
"foo bar foobar".gsub(/\s+/, ' ')
=> "foo bar foobar"
This matches every whitespace, as you only want to replace spaces, use / +/ instead of /\s+/.
"foo bar \nfoobar".gsub(/ +/, ' ')
=> "foo bar \nfoobar"