# Noob question on strings

**URL:** <https://www.ruby-forum.com/t/noob-question-on-strings/170960>\
**Category:** Ruby\
**Created:** [June 24, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960 "2009-06-24T14:19:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 24, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/1 "2009-06-24T14:19:59Z")

</div>

Would anyone care to explain what happens when the following is  
executed? (I already know what gets printed, but not why…)

print \<\<-STRING1, \<\<-STRING2  
Concat  
STRING1  
enate  
STRING2

I’m sorry if it has been answered before, but I didn’t know just what to  
search for in the forum…

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 24, 2009, 2:34pm UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/2 "2009-06-24T14:34:48Z")

</div>

Hi –

On Wed, 24 Jun 2009, Fredrik Ludvigsen wrote:

> I’m sorry if it has been answered before, but I didn’t know just what to  
> search for in the forum…

You’re asking for two strings to be printed. The first is specified as  
“everything starting on the next line and ending at the first  
occurrence of ‘STRING1’ on a line by itself, without including that  
line”. That’s what \<\<-STRING1 means. The second is similarly specified  
with regard to the string ‘STRING2’, but starts after the STRING1  
string has already consumed the “Concat” line.

This construct is called a “here-document”. You’ve come up with a  
fairly arcane use of it 🙂

David

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 25, 2009, 7:05am UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/3 "2009-06-25T07:05:59Z")

</div>

Fredrik Ludvigsen wrote:

> Would anyone care to explain what happens when the following is  
> executed? (I already know what gets printed, but not why…)
> 
> print \<\<-STRING1, \<\<-STRING2  
> Concat  
> STRING1  
> enate  
> STRING2
> 
> I’m sorry if it has been answered before, but I didn’t know just what to  
> search for in the forum…
> 
> Thanks in advance 🙂

1. Knowing what that code does will not make you a better ruby  
programmer.

2. You should never use such a construct in your own code.

If that code is from some tricky quiz question that you have to answer,  
then ok. But you can safely forget all about that construct the  
momement you are done answering the question. What that prints is  
totally irrelevant.

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 25, 2009, 10:02am UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/4 "2009-06-25T10:02:37Z")

</div>

7stud – wrote:

> 1. Knowing what that code does will not make you a better ruby  
> programmer.
> 
> 2. You should never use such a construct in your own code.
> 
> If that code is from some tricky quiz question that you have to answer,  
> then ok. But you can safely forget all about that construct the  
> momement you are done answering the question. What that prints is  
> totally irrelevant.

Got it. I’m just a little uncomfortable with not understanding the code  
examples that I read 🙂

Thanks for the good answers btw.

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 25, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/5 "2009-06-25T13:41:33Z")

</div>

Hi –

On Thu, 25 Jun 2009, Fredrik Ludvigsen wrote:

> Got it. I’m just a little uncomfortable with not understanding the code  
> examples that I read 🙂

You’ve got the right attitude. As a Ruby programmer, you certainly  
want to be able to understand every single line of Ruby code you come  
across. There’s no such thing as a construct that it isn’t a good idea  
to understand.

David

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 26, 2009, 3:15am UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/6 "2009-06-26T03:15:59Z")

</div>

7stud – wrote:

> > 
> 
> If that code is from some tricky quiz question that you have to answer,  
> then ok. But you can safely forget all about that construct the  
> momement you are done answering the question. What that prints is  
> totally irrelevant.

Is there a valid reason not to use something like:

print \<\<-Stop  
#{a+b}  
Stop

or the version posted by the OP? Compared to some of the other code I  
see in Ruby this seems fairly easy to understand once I experimented  
with it.

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 26, 2009, 3:58am UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/7 "2009-06-26T03:58:49Z")

</div>

On Jun 25, 2009, at 18:25 , David A. Black wrote:

> There’s absolutely nothing wrong with using here-documents. I would,  
> however, normally avoid the double-barreled one (the one that the OP  
> was asking about). It’s quite clear what it does, once you know how  
> here-docs work, but I don’t like the two here-docs themselves in quick  
> sequence like that, especially if they’re longer (which they almost  
> certainly would be). It would be a (minor) nuisance to have to parse  
> the two of them visually to locate the first delimiter. I imagine  
> there’s always a somewhat clearer way.

don’t say two… it isn’t limited to two… it is N!

method \<\<-END1.kill, \<\<-END2.me, \<\<-ENDN.now!  
omg

```
    END1

```

this

```
END2

      _sucks_

                                                  ENDN

```

I wish I could say that wasn’t valid ruby…

echo $GAH | parse\_tree\_show  
s(:call,  
nil,  
:method,  
s(:arglist,  
s(:call, s(:str, " omg \n\n"), :kill, s(:arglist)),  
s(:call, s(:str, “\n this\n\n”), :me, s(:arglist)),  
s(:call, s(:str, “\n _sucks_\n\n”), :now!, s(:arglist))))

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 26, 2009, 3:26am UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/8 "2009-06-26T03:26:33Z")

</div>

Hi –

On Fri, 26 Jun 2009, Michael W. Ryder wrote:

> > > 
> > 
> > If that code is from some tricky quiz question that you have to answer,  
> > or the version posted by the OP? Compared to some of the other code I see in  
> > Ruby this seems fairly easy to understand once I experimented with it.

There’s absolutely nothing wrong with using here-documents. I would,  
however, normally avoid the double-barreled one (the one that the OP  
was asking about). It’s quite clear what it does, once you know how  
here-docs work, but I don’t like the two here-docs themselves in quick  
sequence like that, especially if they’re longer (which they almost  
certainly would be). It would be a (minor) nuisance to have to parse  
the two of them visually to locate the first delimiter. I imagine  
there’s always a somewhat clearer way.

David

---

<div class="post-metadata">

**Author:** ![styggentorsken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@styggentorsken](https://www.ruby-forum.com/u/styggentorsken)\
**Post date:** [June 26, 2009, 4:06am UTC](https://www.ruby-forum.com/t/noob-question-on-strings/170960/9 "2009-06-26T04:06:26Z")

</div>

Hi –

On Fri, 26 Jun 2009, Ryan D. wrote:

> > there’s always a somewhat clearer way.
> 
> don’t say two… it isn’t limited to two… it is N!

I figured two would be understood to encompass 3+ also 🙂

> method \<\<-END1.kill, \<\<-END2.me, \<\<-ENDN.now!

And don’t forget the ever (un)popular:

def method(\*); end  
a=b=d=e=1  
c = []  
res = method(a,b,c\<\<d,\<\<d,e) # 🙂  
Hi!  
d

David
