为什么ruby的IO.read添加换行符?

Chr*_*s B -1 ruby io

$ echo testing > testfile
$ irb
2.5.1 :001 > IO.read('testfile')
=> "testing\n"
Run Code Online (Sandbox Code Playgroud)

试图了解换行符的来源,因为它显然不在文件中.

mu *_*ort 7

但是新行在文件中,echo添加它.你可以用hexdump看看自己:

$ echo testing > testfile
$ hexdump testfile 
0000000 74 65 73 74 69 6e 67 0a                        
0000008
Run Code Online (Sandbox Code Playgroud)

0x0a是你的换行符.

你可以问你的shell(大概bash)echo:

$ help echo
echo: echo [-neE] [arg ...]
    Output the ARGs.  If -n is specified, the trailing newline is
    suppressed. [...]
Run Code Online (Sandbox Code Playgroud)

所以,如果你说echo -n testing > testfile,你会得到你期望的结果.