Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>**** This answer lead to a new gem: <a href="http://rubygems.org/gems/file_with_bom" rel="noreferrer">file_with_bom</a> ****</p> <p>I had the similar problem in the past and I extended <code>File.open</code> with additional encoding variants for the <code>w</code>-mode:</p> <pre><code>class File BOM_LIST_hex = { Encoding::UTF_8 =&gt; "\xEF\xBB\xBF", #"\uEFBBBF" Encoding::UTF_16BE =&gt; "\xFE\xFF", #"\uFEFF", Encoding::UTF_16LE =&gt; "\xFF\xFE", Encoding::UTF_32BE =&gt; "\x00\x00\xFE\xFF", Encoding::UTF_32LE =&gt; "\xFE\xFF\x00\x00", } BOM_LIST_hex.freeze def utf_bom_hex(encoding = external_encoding) BOM_LIST_hex[encoding] end class &lt;&lt; self alias :open_old :open def open(filename, mode_string = 'r', options = {}, &amp;block) #check for bom-flag in mode_string options[:bom] = true if mode_string.sub!(/-bom/i,'') f = open_old(filename, mode_string, options) if options[:bom] case mode_string #r|bom already standard since 1.9.2 when /\Ar/ #read mode -&gt; remove BOM #remove BOM bom = f.read(f.utf_bom_hex.bytesize) #check, if it was really a bom if bom != f.utf_bom_hex.force_encoding(bom.encoding) f.rewind #return to position 0 if BOM was no BOM end when /\Aw/ #write mode -&gt; attach BOM f = open_old(filename, mode_string, options) f &lt;&lt; f.utf_bom_hex.force_encoding(f.external_encoding) end #mode_string end if block_given? yield f f.close end end end end #File </code></pre> <p>Testcode:</p> <pre><code>EXAMPLE_TEXT = 'some content öäü' File.open("file_utf16le.txt", "w:utf-16le|bom"){|f| f &lt;&lt; EXAMPLE_TEXT } File.open("file_utf16le.txt", "r:utf-16le|bom:utf-8"){|f| p f.read } File.open("file_utf16le.txt", "r:utf-16le:utf-8", :bom =&gt; true ){|f| p f.read } File.open("file_utf16le.txt", "r:utf-16le:utf-8"){|f| p f.read } File.open("file_utf8.txt", "w:utf-8", :bom =&gt; true ){|f| f &lt;&lt; EXAMPLE_TEXT } File.open("file_utf8.txt", "r:utf-8", :bom =&gt; true ){|f| p f.read } File.open("file_utf8.txt", "r:utf-8|bom", ){|f| p f.read } File.open("file_utf8.txt", "r:utf-8", ){|f| p f.read } </code></pre> <p>Some remarks:</p> <ul> <li>The code is from pre 1.9-times (but it still works).</li> <li>I used <code>-bom</code> as a bom indicator (ruby 1.9 uses <code>|bom</code>.</li> </ul> <p>Some needed fixes to be better:</p> <ul> <li>use <code>|bom</code> instead <code>-bom</code></li> <li>use the standard <code>r|bom</code> for reading</li> <li>make it ruby 1.8 and 1.9 enabled</li> </ul> <p>Perhaps I will find some time tomorrow to refactor my code and provide it as a gem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload