I am trying to write a ruby string to a file in such a way that any newline characters embedded in the string remain embedded. This is being written out to a file which will then be processed by another tool.
我正在尝试将ruby字符串写入文件,以便嵌入字符串中的任何换行符都保持嵌入状态。这是写入文件,然后由另一个工具处理。
An example is below.
一个例子如下。
I want this: 1 [label="this is a\ntest"] \n (second \n is a true newline)
I have tried this: string = '1 [label="this is a\ntest"]' + "\n"
Any thoughts?
3 个解决方案
#1
2
I figured it out. I was using rails word_wrap helper function to wrap text and that was using "\n" and not '\n'. I copied the function and wrote my own.
我想到了。我使用rails word_wrap辅助函数来包装文本,并使用“\ n”而非“\ n”。我复制了这个函数并编写了自己的函数。
Good to go now.
很高兴现在去。
#2
1
In Ruby, single quotes don't interpret escape sequences. You could use the quote operator:
在Ruby中,单引号不解释转义序列。您可以使用引用运算符:
string = %Q/1 [label="this is a\\ntest"] \n/
The character after %Q is the string beginning, ending delimiter. ruby intelligently uses paired characters that have open close equivalents. e.g.
%Q之后的字符是字符串开头,结束分隔符。 ruby智能地使用具有开放近似等价物的成对字符。例如
string = %Q{1 [label="this is a\\ntest"] \n}
string = %Q(1 [label="this is a\\ntest"] \n)
string = %Q!1 [label="this is a\\ntest"] \n!
This lets you choose a convenient delimiter doesn't need additional escaping for a particular string literal.
这使您可以选择一个方便的分隔符,不需要为特定的字符串文字进行额外的转义。
#3
0
the first newline in the example above is read as a real newline by the downstream processing tool.
下面的示例中的第一个换行符被下游处理工具读取为真实的换行符。
This sounds like your problem, then. Your processing tool is taking the literal whack-n (aka \x5c\x6e) and translating it to a single \x0a).
这听起来像你的问题。您的处理工具正在使用文字whack-n(又名\ x5c \ x6e)并将其翻译为单个\ x0a)。
In other words, your output process is fine -- it's your input process is unsafe. You can check your string like so:
换句话说,您的输出过程很好 - 这是您的输入过程是不安全的。您可以像这样检查字符串:
string.split(//).map {|x| x[0].ord.to_s(16)}
You'll notice there's only one "a" there, at the end.
你会注意到最后只有一个“a”。
#1
2
I figured it out. I was using rails word_wrap helper function to wrap text and that was using "\n" and not '\n'. I copied the function and wrote my own.
我想到了。我使用rails word_wrap辅助函数来包装文本,并使用“\ n”而非“\ n”。我复制了这个函数并编写了自己的函数。
Good to go now.
很高兴现在去。
#2
1
In Ruby, single quotes don't interpret escape sequences. You could use the quote operator:
在Ruby中,单引号不解释转义序列。您可以使用引用运算符:
string = %Q/1 [label="this is a\\ntest"] \n/
The character after %Q is the string beginning, ending delimiter. ruby intelligently uses paired characters that have open close equivalents. e.g.
%Q之后的字符是字符串开头,结束分隔符。 ruby智能地使用具有开放近似等价物的成对字符。例如
string = %Q{1 [label="this is a\\ntest"] \n}
string = %Q(1 [label="this is a\\ntest"] \n)
string = %Q!1 [label="this is a\\ntest"] \n!
This lets you choose a convenient delimiter doesn't need additional escaping for a particular string literal.
这使您可以选择一个方便的分隔符,不需要为特定的字符串文字进行额外的转义。
#3
0
the first newline in the example above is read as a real newline by the downstream processing tool.
下面的示例中的第一个换行符被下游处理工具读取为真实的换行符。
This sounds like your problem, then. Your processing tool is taking the literal whack-n (aka \x5c\x6e) and translating it to a single \x0a).
这听起来像你的问题。您的处理工具正在使用文字whack-n(又名\ x5c \ x6e)并将其翻译为单个\ x0a)。
In other words, your output process is fine -- it's your input process is unsafe. You can check your string like so:
换句话说,您的输出过程很好 - 这是您的输入过程是不安全的。您可以像这样检查字符串:
string.split(//).map {|x| x[0].ord.to_s(16)}
You'll notice there's only one "a" there, at the end.
你会注意到最后只有一个“a”。