[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Heredoc and array / hash construction syntax error

email55555 email55555

3/16/2005 10:20:00 PM

My question is how can you construct a "heredoc array" ( or hash).

Let me start with is code:
a = [
1,
2
]

it is perfect ruby syntax.

but if you put the ',' on next line like:
a = [
1
,
2
]
then you got syntax error.

It seems stupid to put ',' on next line but I have no choice,
here is my use case with heredoc example:
a = [
<<EOD1
abc
very very long string and many many lines ...
EOD1
,
<<EOD2
123
very very long string and many many lines ...
EOD2
]

I cannot put the ',' just after EOD1, it must to the next line,
if not, the "EOD1," is not consider termination of heredoc.
but put the ',' on next line then I got syntax error ...


How do you resolve this problem?

Thank you.


6 Answers

Brian Mitchell

3/16/2005 10:44:00 PM

0

On Thu, 17 Mar 2005 07:19:41 +0900, David Tran <email55555@gmail.com> wrote:
> My question is how can you construct a "heredoc array" ( or hash).
>
> Let me start with is code:
> a = [
> 1,
> 2
> ]
>
> it is perfect ruby syntax.
>
> but if you put the ',' on next line like:
> a = [
> 1
> ,
> 2
> ]
> then you got syntax error.
>
> It seems stupid to put ',' on next line but I have no choice,
> here is my use case with heredoc example:
> a = [
> <<EOD1
> abc
> very very long string and many many lines ...
> EOD1
> ,
> <<EOD2
> 123
> very very long string and many many lines ...
> EOD2
> ]
>
> I cannot put the ',' just after EOD1, it must to the next line,
> if not, the "EOD1," is not consider termination of heredoc.
> but put the ',' on next line then I got syntax error ...
>
> How do you resolve this problem?
>
> Thank you.
>
>

Depending on what the test is, a %Q{ } pair might work better. You
will have to escape any '}'.

a = [
%Q{Multi line
white space is kept.
My here doc clone "can have quotes" 'of' any kind.},

%Q{just remember to use a \} to close
your text}
]

Personally, I don't care for heredoc format so i don't use it often.

Brian.


Eric Hodel

3/16/2005 10:44:00 PM

0


On 16 Mar 2005, at 14:19, David Tran wrote:

> My question is how can you construct a "heredoc array" ( or hash).
>
> Let me start with is code:
> a = [
> 1,
> 2
> ]
>
> it is perfect ruby syntax.
>
> but if you put the ',' on next line like:
> a = [
> 1
> ,
> 2
> ]
> then you got syntax error.
>
> It seems stupid to put ',' on next line but I have no choice,
> here is my use case with heredoc example:

Like this:

$ ruby
a = [
<<EOD,
abc
stuff
EOD
]

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

email55555 email55555

3/17/2005 3:21:00 AM

0

> Like this:
>
> $ ruby
> a = [
> <<EOD,
> abc
> stuff
> EOD
> ]

It works.
Thanks Eric.


Relm

3/17/2005 11:17:00 AM

0

Robert Klemme

3/17/2005 1:24:00 PM

0


"Relm" <relm@3tlk.net> schrieb im Newsbeitrag
news:Pine.LNX.4.21.0503170235060.3371-100000@frieza...
> On Thu, 17 Mar 2005, David Tran wrote:
>
> > It seems stupid to put ',' on next line but I have no choice,
> > here is my use case with heredoc example:
> > a = [
> > <<EOD1
> > abc
> > very very long string and many many lines ...
> > EOD1
> > ,
> > <<EOD2
> > 123
> > very very long string and many many lines ...
> > EOD2
> > ]
> >
> > I cannot put the ',' just after EOD1, it must to the next line,
> > if not, the "EOD1," is not consider termination of heredoc.
> > but put the ',' on next line then I got syntax error ...
>
> It may be more readable to put the expression on one line with all the
> heredocs stacked beneath:
>
> a = [<<EOD1, <<EOD2]
> abc
> very very long string and many many lines ...
> EOD1
> 123
> very very long string and many many lines ...
> EOD2

This works also:

a = [
<<EOD1,
abc
very very long string and many many lines ...
EOD1
<<EOD2,
123
very very long string and many many lines ...
EOD2
]

Note: "<<EOD1" is the expression that is replaced by the string, hence the
"," directly after it.

Kind regards

robert

Eric Hodel

3/17/2005 5:39:00 PM

0

On 17 Mar 2005, at 03:16, Relm wrote:

> It may be more readable to put the expression on one line with all the
> heredocs stacked beneath:

I find that type of code gives me rather intense feelings of disgust
and loathing.

I found such an example in mkmf.rb and it was quite confusing to me,
because the first here-doc terminators were nearly indistinguishable
from the rest of the contents of the file.

The biggest problem with beginning multiple here-docs on a single line
is that you can have no whitespace between the end of the first and the
beginning of the second unless that whitespace is supposed to be in the
here-docs.

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04