tags:

views:

45

answers:

3

If I create a Here document:

myheredoc = <<HTMLOUTPUT
<div>This is the div</div>
HTMLOUTPUT

Can I use 'myheredoc' to manipulate this Here document like a regular string?

+5  A: 

Sure can. The syntax is there to make it easier to read, you are still just creating a string.

>> 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>"
Michael Sepcot
+3  A: 

heredoc is just a syntax for generating a string. Therefore you can use all standard string methods. eg:

replaceddoc = myheredoc.gsub(/div/, 'replaced div')
dasil003
A: 

There are a number of ways of declaring strings:

  • Unescaped: 'foo' or %q[foo] or here-doc style <<MARKER
  • Escaped: "foo" or %Q[foo] or here-doc style <<"MARKER"

In all cases the strings are editable, not frozen, so yes, they can be modified after the fact.

tadman