My code works perfectly on Linux. When I move the code over to Windows, Ruby automatically converts all "\r\n" line endings to "\n" which makes all my strings one character too short, which in turn leads to errors when I use the line length for File#seek
calls.
我的代码在Linux上完美运行。当我将代码移动到Windows时,Ruby会自动将所有“\ r \ n”行结尾转换为“\ n”,这会使我的所有字符串中的一个字符太短,这会导致我使用文件行长度时出错#seek来电。
Rather than working around the issue by seeking by one extra character or converting the line endings back, I'd rather just prevent Ruby from changing them in the first place. Can that be done?
通过寻找一个额外的角色或将线路结尾转换回来解决问题,我宁愿只是阻止Ruby首先改变它们。可以这样做吗?
1 个解决方案
#1
0
I had assumed that the read mode "rb" would force "ASCII-8BIT" encoding of the input. Reading the docs a bit closer, it turns out that I can both use the "rb" read mode to disable CRLF line endings and explicitly pass an internal and external encodings like normal.
我曾假设读取模式“rb”会强制输入“ASCII-8BIT”编码。更接近地阅读文档,事实证明我可以使用“rb”读取模式来禁用CRLF行结束并显式传递内部和外部编码,就像正常一样。
In other words, a perfect solution with no extra work involved.
换句话说,一个完美的解决方案,无需额外的工作。
#1
0
I had assumed that the read mode "rb" would force "ASCII-8BIT" encoding of the input. Reading the docs a bit closer, it turns out that I can both use the "rb" read mode to disable CRLF line endings and explicitly pass an internal and external encodings like normal.
我曾假设读取模式“rb”会强制输入“ASCII-8BIT”编码。更接近地阅读文档,事实证明我可以使用“rb”读取模式来禁用CRLF行结束并显式传递内部和外部编码,就像正常一样。
In other words, a perfect solution with no extra work involved.
换句话说,一个完美的解决方案,无需额外的工作。