Print <<XXX parses some text in scope ending with XXX -- Why?

As I read Thomas&Hunt p. 205, Ruby should build a quoted string with the
text from <<XXX to the next XXX. But that doesn’t happen with some quoted
strings in that block, as follows. Why?

F:_Projects_Current_Projects_Ruby\A009-Comments\Comments.rb

for i in 1…3
puts i.to_s
end

print <<TEST
xxxx
yyyy
“abc”

class A
def initialize(*args, &block)
puts ‘—’
puts “args <#{ args.inspect }>”
block.call if block
end
end
TEST

<<OUTPUT

ruby Comments.rb
Test
1
2
3
Comments.rb:15: undefined local variable or method `args’ for main:Object
(NameError)
Exit code: 1
OUTPUT

···

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 5/1/2004

OK, I found a reasonable workaround: I’m developing in SciTE, so I
selected the XXX block and replaced #{ in the selection with \#{, which
should be easy to undo safely. I still think there should be someway to
achieve the C equivalent of /* … */.

···

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.665 / Virus Database: 428 - Release Date: 5/1/2004

In a normal heredoc, you can do variable interpolation:

a = “foo”
=> “foo”
str = <<TEXT
text #{a} here
TEXT
=> “text foo here\n”

You can get around this in two ways. One, mentioned by a previous
poster, is to escape the interpolation:

str = <<TEXT
text #{a} here
TEXT
=> “text #{a} here\n”

Another way, if you aren’t going to need interpolation in that
particular string, is to use a non-interpolated heredoc:

str = <<‘TEXT’
text #{a} here
TEXT
=> “text #{a} here\n”

Single quotes around the heredoc word will make it non-interpolated.

HTH,
–Mark

···

On May 2, 2004, at 10:00 PM, Richard Lionheart wrote:

As I read Thomas&Hunt p. 205, Ruby should build a quoted string with
the
text from <<XXX to the next XXX. But that doesn’t happen with some
quoted
strings in that block, as follows. Why?

There is:

=begin
comments here
many lines of them
as many as you want
=end

It’s not used much for normal comments anymore (that I’ve seen) but I
use it for commenting out code often.

···

On May 2, 2004, at 10:09 PM, Richard Lionheart wrote:

OK, I found a reasonable workaround: I’m developing in SciTE, so I
selected the XXX block and replaced #{ in the selection with \#{,
which
should be easy to undo safely. I still think there should be someway
to
achieve the C equivalent of /* … */.

Hi Mark,

Single quotes around the heredoc word will make it non-interpolated.

I’ve seen << used both with and without single & double quotes, so I
mistakenly assumed “there wasn’t a dime’s worth of difference”, as someone
once said about out political parties.

I feel somewhat exhonerated of stupidity by noting that that Thomas&Hunt
discuss Here Documents in two places, pp. 52. 204, but a quick reread
doesn’t reveal the nugget you reported … unless I misread it again :frowning:

Thankfully, we programmers have the Internet and this NG nowadays!!

Regards,
Richard

···

Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.675 / Virus Database: 437 - Release Date: 5/3/2004

“Richard Lionheart” NoOne@Nowhere.com writes:

I feel somewhat exhonerated of stupidity by noting that that Thomas&Hunt
discuss Here Documents in two places, pp. 52. 204, but a quick reread
doesn’t reveal the nugget you reported …

Look on page 204 again:

“If a quoted string was used to specify the terminator, its quoting
rules will be applied to the here-document; otherwise, double-quoting
rules apply.” It doesn’t jump out at you, but it’s there.

Incidentally, Ruby borrowed the here-document behavior, complete with
quoting rules, from Perl. Perl’s here-documents were, in turn, inspired by
those of the UNIX shell, which also distinguishes based on the quoting of the
terminator. The main difference in the shell is that a here document
is treated as the contents of a file fed as input to a command,
rather than as a string specified on the command line.

Thankfully, we programmers have the Internet and this NG nowadays!!

Indeed!

-(another) Mark