С ruby 1.9.2 вы можете использовать режим r:bom|utf-8
text_without_bom = nil #define the variable outside the block to keep the data
File.open('file.txt', "r:bom|utf-8"){|file|
text_without_bom = file.read
}
или
text_without_bom = File.read('file.txt', encoding: 'bom|utf-8')
или
text_without_bom = File.read('file.txt', mode: 'r:bom|utf-8')
Он не делает Не имеет значения, если спецификация доступна в файле или нет.
Вы также можете использовать опцию кодирования с другими командами:
text_without_bom = File.readlines(@filename, "r:utf-8")
(вы получаете массив со всеми строками).
Или с CSV:
require 'csv'
CSV.open(@filename, 'r:bom|utf-8'){|csv|
csv.each{ |row| p row }
}