开发者

How to write just one 0x0A character to STDOUT in Ruby on Windows?

How to write just one 0x0A character to STDOUT in Ruby on Windows. Just one, without 0x0D.

@EDIT: Thanks to all! I knew about binary/text mode concept but have no idea how to deal with this in Ruby interpreter. The solution is 开发者_JAVA技巧to use STDOUT.binmode.

There is an open question how than switch STDOUT back to text mode using platform independent code.


This is due to the "virtual newline". At the (Ruby) IO layer this feature is responsible for mapping "\n" (0x0A, LF) to/from the Operating Systems definition of a newline. Windows maps "\n" to CR+LF (whereas "\n" is mapped unchanged to LF in Unix-like systems).

Use ios.binmode to put the stream into binary mode which disables the "virtual newline" mode. This is also very important to do for dealing with binary streams such as images ;-)

Happy coding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜