heredoc with variable interpolation breaks script

O

Oliver Heins

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFBSjzDwe2dZoA2CcRAkiQAJ4uISXVND5lZYkcDvUz580zS4RA1wCePrsD
dpoCV+C+nWX8DWkWdEFGeD8=
=H75A
-----END PGP SIGNATURE-----
 
L

Logan Capaldo

I have a somewhat weird problem: Since a few days (I recognized
yesterday) a script ceased to work which did perfectly before. I
stripped it down to a heredoc with string interpolation, which now
breaks the script. Here's a minimal example:



#!/usr/bin/ruby

var1 = "test"
var2 = <<EOF
let us look #{test}
EOF
puts var2
You are calling the method "test" (which is a perlism, FYI) inside your
HEREDOC. I'm pretty sure this was just a typo, and you meant to say
#{var1}, not #{test}.
Running this script, I get the following error:

,----
| olli@athene:~/tmp$ ./test.rb
| ./test.rb:5:in `test': wrong number of arguments (ArgumentError)
| from ./test.rb:5
| olli@athene:~/tmp$ ruby --version
| ruby 1.8.4 (2005-12-24) [i486-linux]
`----

I'm using ruby on a Debian unstable system.

TIA,
olli
 
O

Oliver Heins

Logan Capaldo said:
You are calling the method "test" (which is a perlism, FYI) inside your
HEREDOC. I'm pretty sure this was just a typo, and you meant to say
#{var1}, not #{test}.

Indeed, it was a typo. The real error was that I did a chomp on an
undefined array-entry. I create the array key on the fly and was just
presuming that the source I was parsing contained the specific entry.
As the source changed, my script failed. I now initialize the array
keys with an empty string and everything works fine.

Thanks for the hint.
olli
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top