Is there a way in ruby 1.9 to remove invalid byte sequences from strings? Is there a way in ruby 1.9 to remove invalid byte sequences from strings? ruby ruby

Is there a way in ruby 1.9 to remove invalid byte sequences from strings?


"€foo\xA0".encode('UTF-16le', invalid: :replace, replace: '').encode('UTF-8')


"€foo\xA0".chars.select(&:valid_encoding?).join


Ruby 2.0 and 1.9.3

"€foo\xA0".encode(Encoding::UTF_8, Encoding::UTF_8, :invalid => :replace)

Ruby 2.1+

"€foo\xA0".scrub