title: Why do I get an "Invalid Byte Sequence in UTF-8" error reading a text file?
body: <p>I'm writing a Ruby script to process a large text file, and keep getting an odd encoding error.
  Here's the situation:</p>
  
  <pre><code>input_data = File.new(in_path, 'r').read
  p input_data.encoding.name   #   UTF-8 
  break_char = "\r".encode("UTF-8")
  p break_char # "\r"
  p break_char.encoding.name # "UTF-8" 
  input_data.split(",".encode("UTF-8"))
  p Encoding.compatible?(input_data, break_char) # # Encoding:UTF-8&gt;
  </code></pre>
  
  <p>This produces the error <code>:in 'split': invalid byte sequence in UTF-8 (ArgumentError)</code></p>
  
  <p>I read <a href="http://blog.grayproductions.net/articles/ruby_19s_string" rel="nofollow">http://blog.grayproductions.net/articles/ruby_19s_string</a> and looked at other solutions to apparently the same problem, but still can't work out why it's happening when I believe I am controlling the encodings.</p>
  
  <p>I'm on OSX working with ruby 1.9.2</p>