title: Ruby: Binary String to IO
body: <p>I have a string of binary data and I need it as an IO object. So I tried this:</p>
  
  <pre><code>r, w = IO.pipe()
  w &lt;&lt; data
  </code></pre>
  
  <p>But it fails with this error:</p>
  
  <pre><code>Encoding::UndefinedConversionError ("\xD0" from ASCII-8BIT to UTF-8)
  </code></pre>
  
  <p>Why is it trying to convert to UTF-8 in the first place? is there a way to force the IO::pipe method to a binary mode?</p>
  
  <h2>More details:</h2>
  
  <p>I'm trying to read binary data (which is an Excel file) from MongoDB using the Mongoid driver, and then convert it to an IO object in order to use the Spreadsheet gem to read it. Spreadsheet#open expects either a file path or an IO object. </p>
  
  <p>Here's how my file document looks:</p>
  
  <pre><code>class ImportedFile
      include Mongoid::Document
  
      field :file_name, type: String
      field :binary_content, type: Moped::BSON::Binary
  end
  </code></pre>
  
  <p>Here's how I saved the binary data in the first place:</p>
  
  <pre><code>imported_file = ImportedFile.new
  imported_file.file_name = uploaded_file.original_filename
  imported_file.binary_content = Moped::BSON::Binary.new(:generic, uploaded_file.read)
  imported_file.save
  </code></pre>
  
  <p>And here's how I'm trying to read it (doesn't work):</p>
  
  <pre><code>imported_file = ImportedFile.find(file_id)
  
  r, w = IO.pipe()
  w &lt;&lt; imported_file.binary_content.data
  book = Spreadsheet.open r
  </code></pre>