MrPizzaFace
MrPizzaFace

Reputation: 8086

Ruby removing whitespace?

Having some issues removing   whitespace.

vehicle = [" 2013 ", "BMW ", "535 ", "Sedan 4 Door "] 
v = vehicle[0]
# => " 2013 "

v[-1].ord.chr
# => "\xA0"

Failed Attempts:

vehicle.map { |d| d.gsub(/\S(\w*)/, '\1') }
# => ["2013", "MW", "35", "edan  oor"] (space gone but so are other characters.)

vehicle.map { |d| d.gsub(/\xA0/, '') }
# => SyntaxError: (irb):340: invalid multibyte escape: /\xA0/

vehicle.map { |d| d.gsub(/#{160.chr}/, '') }
# => Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

Answer from this question works:

vehicle.map { |d| d.gsub("\302\240", ' ').strip }
# => ["2013", "BMW", "535", "Sedan 4 Door"] 

but it doesn't explain why/how. Can someone explain how and why this works? Or suggest an alternative?

Upvotes: 1

Views: 180

Answers (1)

ChristopheD
ChristopheD

Reputation: 116097

You should be probably able to simply use /[[:space:]]/ to match all whitespace (unicode or not).

\302\240 is just the utf8-encoded nbsp representation.

Upvotes: 3

Related Questions