Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Heredoc and array / hash construction syntax error

Reply
Thread Tools

Heredoc and array / hash construction syntax error

 
 
David Tran
Guest
Posts: n/a
 
      03-16-2005
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.


 
Reply With Quote
 
 
 
 
Brian Mitchell
Guest
Posts: n/a
 
      03-16-2005
On Thu, 17 Mar 2005 07:19:41 +0900, 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:
> 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.


 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      03-16-2005
--Apple-Mail-9--438143558
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed


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 - - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-9--438143558
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCOLayMypVHHlsnwQRAierAJwPcQi009uxX1J51q/iN2l7HSlKpwCdHvoR
MX5/I6qMRy7+VbM2HW+0n4M=
=AWJl
-----END PGP SIGNATURE-----

--Apple-Mail-9--438143558--


 
Reply With Quote
 
T55555
Guest
Posts: n/a
 
      03-17-2005
> Like this:
>
> $ ruby
> a = [
> <<EOD,
> abc
> stuff
> EOD
> ]


It works.
Thanks Eric.


 
Reply With Quote
 
Relm
Guest
Posts: n/a
 
      03-17-2005
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

--
Relm








 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-17-2005

"Relm" <> schrieb im Newsbeitrag
newsine.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

 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      03-17-2005
--Apple-Mail-14--370089524
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

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 - - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-14--370089524
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCOcCJMypVHHlsnwQRAuIOAKD45BDJSLk3VELycooIr9 oaN9V/LwCfa1ut
l1Gc76HECmaIiquEduYHKX8=
=Xpym
-----END PGP SIGNATURE-----

--Apple-Mail-14--370089524--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
eval, HereDoc and Regular Expressions atraver@gmail.com Ruby 3 01-27-2007 09:46 AM
Default construction versus construction with initial values Ook C++ 10 10-08-2005 09:00 PM
heredoc and variables flupke Python 7 06-07-2004 11:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57