[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Problem with "here" document in hash constructor?

john_b_andrews

4/7/2005 4:28:00 PM

In the following code example, constructing hash1 works fine,
but constructing hash2 gives me a hash with only one key:

###########################################################
hash1 = {
"key1" => "value1" ,
"key2" => "value2"
}

p hash1
p hash1.keys.length

hash2 = {
"key1" => <<EOF
value1
EOF,
"key2" => <<EOF
value2
EOF
}

p hash2
p hash2.keys.length
########################################################

If I move the comma separating the two key/value pairs
to its own line (so that EOF is recognized properly), I get
a syntax error:

T2.rb:17: syntax error
T2.rb:18: warning: useless use of a literal in void context
T2.rb:18: syntax error
"key2" => <<EOF
^

Is there any way to do this using "here" documents (<<EOF)
and the Hash {} constructor style? I know I can do it by
constructing the hash another way, but...

Thanks,

JA

3 Answers

Austin Ziegler

4/7/2005 4:37:00 PM

0

On Apr 7, 2005 12:29 PM, john_b_andrews@yahoo.com
<john_b_andrews@yahoo.com> wrote:
> hash2 = {
"key1" => <<EOF,
> value1
> EOF
"key2" => <<EOF,
> value2
> EOF
> }

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


James Gray

4/7/2005 5:09:00 PM

0

On Apr 7, 2005, at 11:29 AM, john_b_andrews@yahoo.com wrote:

> In the following code example, constructing hash1 works fine,
> but constructing hash2 gives me a hash with only one key:

[snip working example]

> hash2 = {
> "key1" => <<EOF
> value1
> EOF,
> "key2" => <<EOF
> value2
> EOF
> }

Try changing that too:

hash2 = {
"key1" => <<EOF,
value1
EOF
"key2" => <<EOF
value2
EOF
}

Hope that helps.

James Edward Gray II



Robert Klemme

4/7/2005 9:44:00 PM

0


"Austin Ziegler" <halostatue@gmail.com> schrieb im Newsbeitrag
news:9e7db911050407093655ee90aa@mail.gmail.com...
> On Apr 7, 2005 12:29 PM, john_b_andrews@yahoo.com
> <john_b_andrews@yahoo.com> wrote:
>> hash2 = {
> "key1" => <<EOF,
>> value1
>> EOF
> "key2" => <<EOF,
>> value2
>> EOF
>> }

Plus a bit of explanation / mnemonic: the "<<EOF" is the expression that's
replaced by the here document. That's why you want the "," directly after
"<<EOF".

Kind regards

robert