I'm... confused

Given:

x = {
:a => “foo”,
:b => [ :c => “c”, :d => “d” ]
}

p x

I get as output:

{:a=>“foo”, :b=>[{:c=>“c”, :d=>“d”}]}

Which is what I WANTED, but not what I expected.

Specifically, the value pointed to by the :b key of x. Does ruby automatically coerce “x => y, …” forms into: “{ x => y, …}” ?

I more or less expected it to be {:a=>“foo”, :b=>[:c, “c”, :d, “d”]}, but maybe that’s my perl background showing.

Hi,

Specifically, the value pointed to by the :b key of x. Does
ruby automatically coerce “x => y, …” forms into: “{ x =>
y, …}” ?

Yes, => always means hash element.

I more or less expected it to be {:a=>“foo”, :b=>[:c, “c”,
:d, “d”]}, but maybe that’s my perl background showing.

More or less, Ruby differs from Perl.

···

At Thu, 29 Jan 2004 12:21:26 +0900, Michael campbell wrote:


Nobu Nakada

Michael you are using hash syntax. Hence the =>. If you supply => Ruby
will treat your array as a hash. If you swap those to a , then you will get
what you were expecting.

Zach

···

-----Original Message-----
From: Michael campbell [mailto:michael_s_campbell@yahoo.com]
Sent: Wednesday, January 28, 2004 10:21 PM
To: ruby-talk ML
Subject: I’m… confused

Given:

x = {
:a => “foo”,
:b => [ :c => “c”, :d => “d” ]
}

p x

I get as output:

{:a=>“foo”, :b=>[{:c=>“c”, :d=>“d”}]}

Which is what I WANTED, but not what I expected.

Specifically, the value pointed to by the :b key of x. Does ruby
automatically coerce “x => y, …” forms into: “{ x => y, …}” ?

I more or less expected it to be {:a=>“foo”, :b=>[:c, “c”, :d, “d”]}, but
maybe that’s my perl background showing.