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
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
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
-
Knowing what that code does will not make you a better ruby
programmer.
-
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.
7stud – wrote:
-
Knowing what that code does will not make you a better ruby
programmer.
-
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.
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
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.
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))))
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
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