我该如何拆分长线?

时间:2012-09-29 02:40:36

标签: ruby-on-rails string erb

我正在做一些erb练习和测试,但我似乎无法找到如何在编辑器中分割一个太长的愚蠢行。我已经尝试过使用\ n和\但在终端中运行它时它不能正常工作。

以下是代码:

erb_string = "

 <h1><%= me[:name] + '\\'s ' + 'Blog'%></h1>

 <ul>
    <% animals.each do |animal| %>
    <li><%= animal.upcase.reverse %></li>
<% end %>
 </ul>

 <p><%= 'My name is ' + me[:name] + ', my eyes are ' + me[:eyes] + '.' %></p>

 <p><%= 'Let\\'s do some numbers! '  + numbers.last.to_s + '! is ' + numbers.inject(:*).to_s %></p> # I want to split this line in my editor because it is too long.
"

3 个答案:

答案 0 :(得分:0)

   <p><%= 'Let\'s do some numbers! '  + \
   numbers.last.to_s + '! is ' + \
   numbers.inject(:*).to_s %></p>

这应该有效。 如果它不起作用,可能是因为你使用两个反斜杠来逃避单引号并且第一个反弹逃脱了第二个,所以单引号没有被转义。

顺便说一句,如果你有一个包含单引号的字符串,你最好用双引号将其封装起来,这样你就不必转义单引号。

答案 1 :(得分:0)

为了能够将\ n用于新行,您的字符串必须是双引号。因此,您需要使用“Hello \ nWorld”

而不是“Hello \ nWorld”

答案 2 :(得分:0)

rb_string = <<EOL

Lot's of cool
ruby an html and javascript stuff
That includes all the happy new lines that you want
so that you can read it.

EOL

EOL和任意字符串。结束EOL必须在一行的开头,并且一直都是。

相关问题