gpt4 book ai didi

ruby - 如何阻止 Ruby 自动添加回车符

转载 作者:可可西里 更新时间:2023-11-01 09:20:02 26 4
gpt4 key购买 nike

我在从 Windows 机器构建要在 Linux 环境中读取的文本文件时遇到问题。

def test

my_file = Tempfile.new('filetemp.txt')

my_file.print "This is on the first line"
my_file.print "\x0A"
my_file.print "This is on the second line"

my_file.close

FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")

end

0A 是换行的 ASCII 代码,但是当我在 Notepad++ 中打开生成的文件时,我看到它在行尾附加了 CR LF .

如何添加换行符作为换行符?

最佳答案

尝试将输出分隔符 $\ 设置为 \n

def test
$\ = "\n"
my_file = Tempfile.new('filetemp.txt')

my_file.print "This is on the first line"
my_file.print "This is on the second line"

my_file.close

FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")

end

或者你应该可以使用#write,它不会添加输​​出分隔符

def test
my_file = Tempfile.new('filetemp.txt')

my_file.write "This is on the first line"
my_file.write "\x0A"
my_file.write "This is on the second line"

my_file.close

FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")

end

关于ruby - 如何阻止 Ruby 自动添加回车符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com