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

  • Thread starter Richard Lionheart
  • Start date
R

Richard Lionheart

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
 
R

Richard Lionheart

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 /* ... */.
 
M

Mark Hubbart

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?

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
 
M

Mark Hubbart

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

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

Richard Lionheart

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

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

Regards,
Richard
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top