title: ruby 1.9, force_encoding, but check
body: <p>I have a string I have read from some kind of input. </p>
  
  <p>To the best of my knowledge, it is UTF8.  Okay:</p>
  
  <pre><code>string.force_encoding("utf8")
  </code></pre>
  
  <p>But if this string has bytes in it that are not in fact legal UTF8, I want to know now and take action. </p>
  
  <p>Ordinarily, will force_encoding("utf8") raise if it encounters such bytes? I <em>believe</em> it will not. </p>
  
  <p>If I was doing an <a href="http://www.ruby-doc.org/core-1.9.3/String.html#method-i-encode" rel="noreferrer">#encode</a> I could choose from the handy options with what to do with characters that are invalid in the source encoding (or destination encoding). </p>
  
  <p>But I'm not doing an #encode, I'm doing a #force_encoding. It has no such options. </p>
  
  <p>Would it make sense to </p>
  
  <pre><code>string.force_encoding("utf8").encode("utf8")
  </code></pre>
  
  <p>to get an exception right away? Normally encoding <em>from</em> utf8 <em>to</em> utf8 doesn't make any sense. But maybe this is the way to get it to raise right away if there's invalid bytes? Or use the <code>:replace</code> option etc to do something different with invalid bytes?</p>
  
  <p>But no, can't seem to make that work either. </p>
  
  <p>Anyone know?</p>
  
  <pre><code>1.9.3-p0 :032 &gt; a = "bad: \xc3\x28 okay".force_encoding("utf-8")
  =&gt; "bad: \xC3( okay"
  1.9.3-p0 :033 &gt; a.valid_encoding?
  =&gt; false
  </code></pre>
  
  <p>Okay, but how do I find and eliminate those bad bytes?  Oddly, this does NOT raise:</p>
  
  <pre><code>1.9.3-p0 :035 &gt; a.encode("utf-8")
   =&gt; "bad: \xC3( okay"
  </code></pre>
  
  <p>If I was converting to a different encoding, it would!</p>
  
  <pre><code>1.9.3-p0 :039 &gt; a.encode("ISO-8859-1")
  Encoding::InvalidByteSequenceError: "\xC3" followed by "(" on UTF-8
  </code></pre>
  
  <p>Or if I told it to, it'd replace it with a "?" =></p>
  
  <pre><code>1.9.3-p0 :040 &gt; a.encode("ISO-8859-1", :invalid =&gt; :replace)
  =&gt; "bad: ?( okay"
  </code></pre>
  
  <p>So ruby's got the smarts to know what are bad bytes in utf-8, and to replace em with something else -- when converting to a different encoding. But I don't <em>want</em> to convert to a different encoding, i want to stay utf8 -- but I might want to raise if there's an invalid byte in there, or I might want to replace invalid bytes with replacement chars. </p>
  
  <p>Isn't there some way to get ruby to do this?</p>
  
  <p><strong>update</strong> I believe this has finally been added to ruby in 2.1, with String#scrub present in the 2.1 preview release to do this. So look for that!</p>