Il giorno Mon, 23 Jan 2012 02:54:17 +0900
Blub B. [email protected] ha scritto:
end
It’s another form of string literal, just as ’ ’ or " ". The string
content
start from the line after STARTCODE and continues until a line starting
with
STARTCODE is found. Note that the line must truly start with STARTCODE,
it
can’t have any leading whitespace (this is the reason you get an error).
If
you want to have whitespace before the closing STARTCODE, you must put a
after the <<:
return <<-STARTCODE
Note that the delimiting string doesn’t need to be STARTCODE, it can be
anything you like, as long as you use the same string at the beginning
and
end of the string.
return <<STARTCODE
(…)
STARTCODE # NO margin left of STARTCODE
end
Careful with comments on that line:
13:33:38 Temp$ ruby19 x.rb
x.rb:6: can’t find string “STARTCODE” anywhere before EOF
x.rb:2: syntax error, unexpected $end, expecting tSTRING_CONTENT or
tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
13:34:12 Temp$ cat -n x.rb
1
2 s=<<STARTCODE
3 x
4 STARTCODE # NO margin left of STARTCODE
5
6 p s
13:34:14 Temp$
x.rb:2: syntax error, unexpected $end, expecting tSTRING_CONTENT or
Cheers
robert
Indeed.
I did some tests now and if I see correctly, both
the EOS (End Of String) and -EOS here doc formats
require that EOS is immediately followed by a
newline in the code.
Thank you
Peter
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.