如何避免heredoc中的最后一个字符

时间:2017-07-20 09:33:50

标签: ruby heredoc

在ruby heredoc中:

[21] pry(main)> a = <<~TEXT
[21] pry(main)*   asd
[21] pry(main)*   asd
[21] pry(main)* TEXT
=> "asd\n" + "asd\n"

它将生成:

\n

它在字符串的末尾生成 <appender name="MyLogger" type="log4net.Appender.RollingFileAppender"> <file value="RenameFileInCode.txt" /> <appendToFile value="true" /> <maximumFileSize value="10MB" /> <maxSizeRollBackups value="5" /> <!-- rollingStyle value="Size" /--> <rollingStyle value="Date" /> <threshold value="DEBUG" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level %logger %newline%message%newline%exception%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="DEBUG" /> <levelMax value="ERROR" /> </filter> </appender> ,如何避免这种情况?

2 个答案:

答案 0 :(得分:10)

作为sagarpandya82 points out,您需要执行其他操作(即chomp)才能删除额外的\n

您可以像这样使用chomp heredoc

a = <<~TEXT.chomp
  asd
  asd
TEXT
#=> "asd\nasd"

答案 1 :(得分:0)

<<-TEXT.squish.squeeze(' ')
  aa
                  b
TEXT
# => "aa b"