Hi --

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

> 7stud -- wrote:
>> 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.
>
> 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.

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

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com