# Heredocs and white space

**URL:** <https://www.ruby-forum.com/t/heredocs-and-white-space/100464>\
**Category:** Ruby\
**Created:** [July 29, 2006, 3:39am UTC](https://www.ruby-forum.com/t/heredocs-and-white-space/100464 "2006-07-29T03:39:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [July 29, 2006, 3:39am UTC](https://www.ruby-forum.com/t/heredocs-and-white-space/100464/1 "2006-07-29T03:39:02Z")

</div>

Is there some reason heredocs fail if there is white space after the  
closing string? Until I realized what was happening this caused me a  
good bit of trouble. Invisible characters shouldn’t cause stuff to fail.

Example:  
s = \<\<-HEREDOC  
bla bla bla  
HEREDOC

This fails with:

heredoc.rb:3: can’t find string “HEREDOC” anywhere before EOF  
heredoc.rb:1: parse error, unexpected $, expecting tSTRING\_CONTENT or  
tSTRING\_DB  
EG or tSTRING\_DVAR or tSTRING\_END

Delete the spaces at the end of the 3rd line and it works.

I looked through the relevant PickAxe sections and didn’t find anything.

Jack

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [July 29, 2006, 3:51am UTC](https://www.ruby-forum.com/t/heredocs-and-white-space/100464/2 "2006-07-29T03:51:30Z")

</div>

Hi –

On Sat, 29 Jul 2006, Jack C. wrote:

> heredoc.rb:3: can’t find string “HEREDOC” anywhere before EOF  
> heredoc.rb:1: parse error, unexpected $, expecting tSTRING\_CONTENT or  
> tSTRING\_DB  
> EG or tSTRING\_DVAR or tSTRING\_END
> 
> Delete the spaces at the end of the 3rd line and it works.
> 
> I looked through the relevant PickAxe sections and didn’t find anything.

The thing is, the mechanism depends on an exact match. Here’s an  
unlikely but possible example of why it might matter. Line 10 is  
“EOM”; line 12 is "EOM ".

irb(main):008:0\> a,b = \<\<EOM, \<\<“EOM "  
irb(main):009:0” abc  
irb(main):010:0" EOM  
irb(main):011:0" def  
irb(main):012:0" EOM  
=\> [“abc\n”, “def\n”]

David

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [July 29, 2006, 4:01am UTC](https://www.ruby-forum.com/t/heredocs-and-white-space/100464/3 "2006-07-29T04:01:20Z")

</div>

Hi –

On Sat, 29 Jul 2006, removed\_email\_address@domain.invalid wrote:

> > bla bla bla
> 
> The thing is, the mechanism depends on an exact match. Here’s an  
> unlikely but possible example of why it might matter. Line 10 is  
> “EOM”; line 12 is "EOM ".
> 
> irb(main):008:0\> a,b = \<\<EOM, \<\<“EOM "  
> irb(main):009:0” abc  
> irb(main):010:0" EOM  
> irb(main):011:0" def  
> irb(main):012:0" EOM =\> [“abc\n”, “def\n”]

Actually I don’t think I answered your question. Looking at your  
example again, it does look odd. “HEREDOCabc” would be ignored,  
whereas "HEREDOC " causes a failure. I’m not sure why.

David

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [July 29, 2006, 5:32am UTC](https://www.ruby-forum.com/t/heredocs-and-white-space/100464/4 "2006-07-29T05:32:46Z")

</div>

On 7/28/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually I don’t think I answered your question. Looking at your  
> example again, it does look odd. “HEREDOCabc” would be ignored,  
> whereas "HEREDOC " causes a failure. I’m not sure why.

Seems to be specific to the “\<\<-” notation.

irb(main):001:0\> s = \<\<HEREDOC  
irb(main):002:0" bla bla bla  
irb(main):003:0" HEREDOC  
irb(main):004:0" HEREDOC  
=\> “bla bla bla\nHEREDOC \n”

with a space at the end of line 3 works as expected.

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

---

<div class="post-metadata">

**Author:** ![giaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a3d4f5/32.png) [@giaco](https://www.ruby-forum.com/u/giaco)\
**Post date:** [July 30, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/heredocs-and-white-space/100464/5 "2006-07-30T23:54:22Z")

</div>

Jack C. wrote:

> Delete the spaces at the end of the 3rd line and it works.

It works then because “HEREDOC” is equal to “HEREDOC”, when the space at  
the end is present it’s "HEREDOC ". I just tried it at the command line  
using bash and when there was a space at the end it did not blow up, it  
merely included the "HEREDOC " as the last line. Removing the space  
caused it to work correctly. I did not try it under perl or php.

–

Music: [http://www.myspace.com/acidredux](http://www.myspace.com/acidredux)

Links: [http://del.icio.us/furlan](http://del.icio.us/furlan)

Home: [http://thispaceavailable.uxb.net/index.html](http://thispaceavailable.uxb.net/index.html)

We are here to laugh at the odds and live our lives so well that Death  
will tremble to take us.  
– Charles Bukowski
