如何在Ruby中将BOM标记写入文件 [英] How to write BOM marker to a file in Ruby
问题描述
我有一些带有拐杖的工作代码,可以将BOM表标记添加到新文件中.
I have some working code with a crutch to add BOM marker to a new file.
#writing
File.open name, 'w', 0644 do |file|
file.write "\uFEFF"
file.write @data
end
#reading
File.open name, 'r:bom|utf-8' do |file|
file.read
end
有什么方法可以自动添加标记,而无需在数据前写入隐含的"\uFEFF"
?可能是File.open name, 'w:bom' # this mode has no effect
之类的东西?
Is there any way to automatically add the marker without writing cryptic "\uFEFF"
before the data? Something like File.open name, 'w:bom' # this mode has no effect
maybe?
推荐答案
A,我认为您的手动方法是必经之路,至少我不知道有更好的方法:
Alas I think your manual approach is the way to go, at least I don't know a better way:
http://blog.grayproductions.net/articles/miscellaneous_m17n_details
引用JEG2的文章:
Ruby 1.9不会自动向您的数据添加BOM表,因此您将 如果需要的话,需要照顾好它.幸运的是,它不是太 艰难的.基本思想只是在 文件的开头.
Ruby 1.9 won't automatically add a BOM to your data, so you're going to need to take care of that if you want one. Luckily, it's not too tough. The basic idea is just to print the bytes needed at the beginning of a file.
这篇关于如何在Ruby中将BOM标记写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!