title: Ruby: parse yaml from ANSI to UTF-8
body: <p><strong>Problem:</strong></p>
  
  <p>I have the yaml file <code>test.yml</code> that can be encoded in UTF-8 or ANSI:</p>
  
  <pre><code>:excel:
    "Test":
      "eins_Ä": :eins
      "zwei_ä": :zwei
  </code></pre>
  
  <p>When I load the file I need it to be encoded in UTF-8 therefore tried to convert all of the Strings:</p>
  
  <pre><code>require 'yaml'
  file = YAML::load_file('C:/Users/S61256/Desktop/test.yml')
  
  require 'iconv'
  CONV = Iconv.new("UTF-8", "ASCII")
  
  class Test
  
      def convert(hash)
          hash.each{ |key, value| 
              convert(value) if value.is_a? Hash
              CONV.iconv(value) if value.is_a? String
              CONV.iconv(key) if key.is_a? String
          }
      end
  
  end
  
  t = Test.new
  converted = t.convert(file)
  
  p file
  p converted
  </code></pre>
  
  <p>But when I try to run this example script it prints:</p>
  
  <p><code>in 'iconv': eins_- (Iconv:IllegalSequence)</code></p>
  
  <p><strong>Questions:</strong></p>
  
  <p><strong>1.</strong> Why does the error show up and how can I solve it?</p>
  
  <p><strong>2.</strong> Is there another (more appropiate) way to get the file's content in UTF-8?</p>
  
  <p><strong>Note:</strong>
  I need this code to be compatible to Ruby 1.8 as well as Ruby 2.2. For Ruby 2.2 I would replace all the Iconv stuff with String::encode, but that's another topic.</p>