Can I append hash to hashes?

S

Siratinee Sukachai

I need to do a hash some kind like this:
Member => {email => Email, name => Name, lastname => Lastname}

But I get a hash in foreach loop so I need to append a hash to hashes.
e.g.
First round: Member => {email => Email}
Second round: Member => {email => Email, name => Name}
Third round: Member => {email => Email, name => Name, lastname =>
Lastname}

Can I do that and how?
In my case I cannot do the array because I need to use this hash to
generate yaml later.
If I do as the array, the formatting what I get in yaml is not what I
need.
Pls help...
 
G

Giampiero Zanchi

maybe I cannot understand your problem (?)

hsh = {:member => {:email => "Email", :name => "Name", :lastname =>
"Lastname"}}
p hsh

internal_h = {:email => "Email", :name => "Name", :lastname =>
"Lastname"}
external_h = {}
external_h[:member] = internal_h
p external_h
 
Q

Quintus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 22.04.2011 09:22, schrieb Siratinee Sukachai:
I need to do a hash some kind like this:
Member => {email => Email, name => Name, lastname => Lastname}

But I get a hash in foreach loop so I need to append a hash to hashes.
e.g.
First round: Member => {email => Email}
Second round: Member => {email => Email, name => Name}
Third round: Member => {email => Email, name => Name, lastname =>
Lastname}

Can I do that and how?
In my case I cannot do the array because I need to use this hash to
generate yaml later.
If I do as the array, the formatting what I get in yaml is not what I
need.
Pls help...

Have a look at the Hash#merge and Hash#merge! methods.

http://www.ruby-doc.org/core/classes/Hash.html#M000759
http://www.ruby-doc.org/core/classes/Hash.html#M000758

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJNsTQQAAoJELh1XLHFkqha4wwH/RbyOJ6I1HZip5oeAORqYfdB
NdZ6g9YNUh3Y/jX9h7UOFptvJIO8E7CMCgP7ptfqk80jOr5AxEzaBAPY3HRqjGIj
O0uIt8FIu078KmNiBeP2dVHMKCUEPHGWrfGV4pOYkTEDBO7pmkF/KTEuggIdxHGC
gSziditMIpdRqSMeDHufscTCx8OrdU05aLNDiuL8nCpf+HS1Ou5Kyww5FBYbhhD5
Il4BKJyRhZTRYtoAXoSyb9nl+2wZ6/yP36ymQ/37GxmIfVpmQys7mjukjy1mVRbf
OPFqfOdhwnavrzhKYuZJvGMHxegQztjOvsDDZ8QvvxcCNP1Fg4EMLcjnckkEOiI=
=k4QL
-----END PGP SIGNATURE-----
 
S

Siratinee Sukachai

Actually, I need to do like in array which I can use << to assign more
value into array. Anyways can I do that in hash?
 
R

Robert Dober

Actually, I need to do like in array which I can use << to assign more
value into array. Anyways can I do that in hash?

Sure there are two ways, the destructive one, in which the receiver is
modified with #update

or the constructive, in which the receiver is not modified, with #merge
=> {:a=>42}

Be aware of the fact, that in case of conflicting keys the values of
the argument are used, this can be handled by the block form of
#update and #merge, as described here
http://www.rubydoc.info/stdlib/core/1.9.2/Hash
HTH
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,970
Messages
2,570,162
Members
46,710
Latest member
bernietqt

Latest Threads

Top