# Problem with stripping leading whitespace in here documents

**URL:** <https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424>\
**Category:** Ruby\
**Created:** [January 25, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424 "2007-01-25T22:26:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 25, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/1 "2007-01-25T22:26:19Z")

</div>

I’m writing a number of test cases. I would like the input data to  
behave like a file, and I would like to include that data within  
the test file (in order to reduce the proliferation of files and  
eliminate the necessity of tracking an additional, separate file  
for each test case). Similar mechanisms would include BASIC’s  
DATA statements, or Perl’s DATA filehandle. I don’t know of an  
analogue in ruby. Any suggestions?

Instead I am using Bourne shell-type here-documents. But, I am  
encountering a problem. The Bourne shell supports the concept of  
stripping leading white space, and Ruby seems to support the same  
notion (from …/doc/ruby18/ruby-man/syntax.html):

```
"If the - [is] placed before the delimiter, then all leading
whitespcae [sic] characters (tabs or spaces) are stripped from
input lines and the line containing [the] delimiter. This
allows here-documents within scripts to be indented in a natural
fashion."

```

which is the same syntax as the Bourne shell.

But, if I run the following script:

# Start of script:

input1 = \<\<EOF  
abc  
def  
EOF

input2 = \<\<-EOF  
ghi  
jkl  
EOF

p input1  
p input2

# End of script.

I get the following output:

“abc\ndef\n”  
“\tghi\n\tjkl\n”

Note that the leading tab has NOT been removed in the here-document  
that should be stripped (input2). (It is a tab in the original file.)

Is this a bug? or am I missing something in the documentation.  
I’m using ‘ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-freebsd6]’.

(And yes, I know how to strip the leading whitespace with a function  
after reading it. I’m particularly addressing the issue with here  
documents.)

Thanks, - dmw

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 25, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/2 "2007-01-25T22:40:21Z")

</div>

On Jan 25, 12:43 pm, removed\_email\_address@domain.invalid (Douglas Wells) wrote:

> notion (from …/doc/ruby18/ruby-man/syntax.html):
> 
> ```
> "If the - [is] placed before the delimiter, then all leading
> whitespcae [sic] characters (tabs or spaces) are stripped from
> input lines and the line containing [the] delimiter. This
> allows here-documents within scripts to be indented in a natural
> fashion."
> 
> ```

Where is this document that you’re referring to? I just checked my  
(win32 installer) version of Ruby 1.8.5, and I have no document named  
‘syntax.html’ anywhere in the tree of files.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 25, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/3 "2007-01-25T22:41:21Z")

</div>

On Jan 25, 12:43 pm, removed\_email\_address@domain.invalid (Douglas Wells) wrote:

> ```
> "If the - [is] placed before the delimiter, then all leading
> whitespcae [sic] characters (tabs or spaces) are stripped from
> input lines and the line containing [the] delimiter. This
> allows here-documents within scripts to be indented in a natural
> fashion."
> 
> ```

Whoa, I’ve never seen that wording. That is entirely at odds with the  
way Ruby behaves; as you note, no whitespace stripping occurs, except  
that the ‘-’ character allows the ENDDOC indicator to be indented.

The lack of functionality that happens to be described in the document  
that you quote above is what caused me to add this RCR:  
[http://www.rcrchive.net/rcrs/2](http://www.rcrchive.net/rcrs/2)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 26, 2007, 2:21am UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/4 "2007-01-26T02:21:47Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
“Phrogz” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> (win32 installer) version of Ruby 1.8.5, and I have no document named  
> ‘syntax.html’ anywhere in the tree of files.

It was installed on my local system (as part of the ports system  
on FreeBSD). A bit of spelunking shows that it is part of the  
‘ruby-man’ port. A bit more shows that it came as part of a tarball  
named ruby-man-1.4.6.tar.gz, indicating that it might be a bit  
long in the tooth. Checking the page on my systems, I see that  
it’s fairly old, with the corresponding table of contents being  
dated February 1998.

On the other hand, it appears to be the latest “Ruby reference  
manual” that I can find. A Google search for a “ruby reference  
manual” leads to  
[Ruby Syntax](http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc),  
which does contain the quoted text that I posted.

- dmw

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 26, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/5 "2007-01-26T19:40:32Z")

</div>

Phrogz wrote:

> The lack of functionality that happens to be described in the document  
> that you quote above is what caused me to add this RCR:  
> [http://www.rcrchive.net/rcrs/2](http://www.rcrchive.net/rcrs/2)

I vote for option 1 of the above proposal, since option 2 would break a  
lot of my current  
heredoc output.

It would also be nice if one could specify how much of the END token  
leading spaces are  
removed (or replaced) from each line of the heredoc. Similar to the  
strip\_output() method  
in… ri? (Can’t remember exactly)

That is, your

class Foo  
def bar  
if whee  
print \<\<+END  
Hello World…  
Are you listening?  
END  
end  
end  
end

produces

Hello World…  
Are you listening?

and then, for example,

class Foo  
def bar  
if whee  
print \<\<+(2)END  
Hello World…  
Are you listening?  
END  
end  
end  
end

produces

Hello World…  
Are you listening?

i.e. the leading spaces are replaced by the number specified after the  
+. (I’m lukewarm  
about the syntax, but you get the idea)

I realise that would be equivalent to

```
     print <<+END
       Hello World...
          Are you listening?
     END

```

but the “\<\<+(n)TOKEN” functionality would be a really nice feature  
because the number of  
indent spaces, n, can then be easily passed into methods interpolated in  
the heredoc that  
produce output you also want to be indented accordingly. I have that  
situation now where  
interpolated strings in heredocs are produced in the called method via  
another heredoc (I  
hope that makes sense.)

cheers,

paulv

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 26, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/6 "2007-01-26T21:30:59Z")

</div>

On Jan 26, 11:36 am, Paul van Delst [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I vote for option 1 of the above proposal, since option 2 would break a lot of my current  
> heredoc output.

[snip additional suggestions and discussion of the RCR]

I’m mighty tempted to ask you for some examples of what your current  
Heredoc output looks like and how the proposal would break it.  
(However, pretend that I didn’t actually just type that.) Instead, I  
think that – to support the RCR process – I should probably ask you  
to create an account at [RCRchive.net](http://RCRchive.net), subscribe to the mailing list for  
that RCR, and post your question again so that I can ask you questions  
there.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 26, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/7 "2007-01-26T21:36:00Z")

</div>

On Jan 25, 5:30 pm, removed\_email\_address@domain.invalid (Douglas Wells) wrote:

> manual" leads tohttp://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.ht…,  
> which does contain the quoted text that I posted.

Interesting. Definitely, as we’ve established, not correct information  
for the current version of Ruby.

Is anyone around and listening who is old enough to remember 1.4, and  
to know if that is how indented heredoc strings worked back then? (I’m  
interested because it’s very similar to my RCR, and I’d love to know  
the reason that the functionality was abandoned, if it was.)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 27, 2007, 8:47am UTC](https://www.ruby-forum.com/t/problem-with-stripping-leading-whitespace-in-here-documents/83424/8 "2007-01-27T08:47:01Z")

</div>

Hi,

At Sat, 27 Jan 2007 05:35:06 +0900,  
Phrogz wrote in [ruby-talk:236308]:

> Is anyone around and listening who is old enough to remember 1.4, and  
> to know if that is how indented heredoc strings worked back then? (I’m

Nothing has ever changed. The document is incorrect definitely.
