如果我创建一个 Here 文档:
myheredoc = <<HTMLOUTPUT
<div>This is the div</div>
HTMLOUTPUT
我可以使用“myheredoc”像普通字符串一样操作这个 Here 文档吗?
当然可以。语法是为了使它更容易阅读,你仍然只是创建一个字符串。
>> myheredoc = <<HTMLOUTPUT
<div>This is the div</div>
HTMLOUTPUT
=> "<div>This is the div</div>\n"
>> myheredoc << "<p>some paragraph</p>"
=> "<div>This is the div</div>\n<p>some paragraph</p>"
heredoc 只是一种用于生成字符串的语法。因此,您可以使用所有标准字符串方法。例如:
replaceddoc = myheredoc.gsub(/div/, 'replaced div')
有多种声明字符串的方法:
在所有情况下,字符串都是可编辑的,而不是冻结的,所以是的,它们可以在事后修改。