Ruby 输出中的转义行中断

Ruby 输出中的转义行中断,ruby,irb,ruby-1.8.7,Ruby,Irb,Ruby 1.8.7,在Ruby 1.8.7上的IRB中,我有一个正在使用的字符串集合,其中包含换行符。当输出这些换行符时,我希望在字符串中显式地看到\r和\n字符。有什么方法可以告诉put转义这些字符,或者类似于put的方法可以实现我想要的吗 请注意,直接计算每个字符串并不令人满意,因为我希望能够执行以下操作: => mystrings.each { |str| puts str.magical_method_to_escape_special_chars } This is\na string in mys

在Ruby 1.8.7上的IRB中,我有一个正在使用的字符串集合,其中包含换行符。当输出这些换行符时,我希望在字符串中显式地看到
\r
\n
字符。有什么方法可以告诉
put
转义这些字符,或者类似于
put
的方法可以实现我想要的吗

请注意,直接计算每个字符串并不令人满意,因为我希望能够执行以下操作:

=> mystrings.each { |str| puts str.magical_method_to_escape_special_chars }
This is\na string in mystrings.
This is another\n\rstring.
我不想这样做:

=> mystrings[0]
"This is\na string in mystrings."
=> mystrings[1]
"This is another\n\rstring."
...
=> mystrings[1000]
"There are a lot of\n\nstrings!"
我可以使用
string#dump
方法:

=> mystrings.each { |str| puts str.dump }
This is\na string in mystrings.
This is another\n\rstring.
根据,
string#dump

生成所有非打印字符替换为的str版本 \nnn符号和所有特殊字符转义

=> mystrings.each { |str| puts str.dump }
This is\na string in mystrings.
This is another\n\rstring.