With ruby ââ1.9.2 you can use r:bom|utf-8 mode
text_without_bom = nil
or
text_without_bom = File.read('file.txt', encoding: 'bom|utf-8')
or
text_without_bom = File.read('file.txt', mode: 'r:bom|utf-8')
It does not matter if the specification is available in the file or not.
You can also use the encoding option with other commands:
text_without_bom = File.readlines(@filename, "r:utf-8")
(You get an array with all the rows).
Or with CSV:
require 'csv' CSV.open(@filename, 'r:bom|utf-8'){|csv| csv.each{ |row| p row } }
knut Oct. 15 '11 at 20:48 2011-10-15 20:48
source share