Noob question on strings

F

Fredrik Ludvigsen

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

David A. Black

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


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
 
7

7stud --

Fredrik said:
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.
 
F

Fredrik Ludvigsen

7stud said:
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.
 
D

David A. Black

Hi --

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

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

Michael W. Ryder

7stud said:
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.
 
D

David A. Black

Hi --

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
 
R

Ryan Davis

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

David A. Black

Hi --

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top