Noob question on strings

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 :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Hi --

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...

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 :slight_smile:

David

···

On Wed, 24 Jun 2009, Fredrik Ludvigsen wrote:

--
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

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 :slight_smile:

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.

···

--
Posted via http://www.ruby-forum.com/\.

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 :slight_smile:

Thanks for the good answers btw.

···

--
Posted via http://www.ruby-forum.com/\.

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 :slight_smile:

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.

Hi --

···

On Thu, 25 Jun 2009, Fredrik Ludvigsen wrote:

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 :slight_smile:

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

--
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

Hi --

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 :slight_smile:

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

···

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

--
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

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))))

···

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.

Hi --

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!

I figured two would be understood to encompass 3+ also :slight_smile:

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) # :slight_smile:
Hi!
d

David

···

On Fri, 26 Jun 2009, Ryan Davis wrote:

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

--
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