Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3362 > unrolled thread

Can I append hash to hashes?

Started bySiratinee Sukachai <ploy.sukachai@gmail.com>
First post2011-04-22 02:22 -0500
Last post2011-04-22 05:43 -0500
Articles 6 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  Can I append hash to hashes? Siratinee Sukachai <ploy.sukachai@gmail.com> - 2011-04-22 02:22 -0500
    Re: Can I append hash to hashes? Giampiero Zanchi <cidza@tin.it> - 2011-04-22 02:40 -0500
    Re: Can I append hash to hashes? Quintus <sutniuq@gmx.net> - 2011-04-22 02:53 -0500
      Re: Can I append hash to hashes? Siratinee Sukachai <ploy.sukachai@gmail.com> - 2011-04-22 03:13 -0500
        Re: Can I append hash to hashes? Robert Dober <robert.dober@gmail.com> - 2011-04-22 04:07 -0500
    Re: Can I append hash to hashes? Siratinee Sukachai <ploy.sukachai@gmail.com> - 2011-04-22 05:43 -0500

#3362 — Can I append hash to hashes?

FromSiratinee Sukachai <ploy.sukachai@gmail.com>
Date2011-04-22 02:22 -0500
SubjectCan I append hash to hashes?
Message-ID<3d3ff00fedc9442b82823de5ba1d77ae@ruby-forum.com>
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...

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#3364

FromGiampiero Zanchi <cidza@tin.it>
Date2011-04-22 02:40 -0500
Message-ID<42766c71b8cc60f46f4679ef0befae51@ruby-forum.com>
In reply to#3362
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3367

FromQuintus <sutniuq@gmx.net>
Date2011-04-22 02:53 -0500
Message-ID<4DB13412.4000108@gmx.net>
In reply to#3362
-----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-----

[toc] | [prev] | [next] | [standalone]


#3368

FromSiratinee Sukachai <ploy.sukachai@gmail.com>
Date2011-04-22 03:13 -0500
Message-ID<484a995bb4105850d3beed82c8e3da73@ruby-forum.com>
In reply to#3367
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?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3372

FromRobert Dober <robert.dober@gmail.com>
Date2011-04-22 04:07 -0500
Message-ID<BANLkTi=YwMA7tPovpbOsa3EcRGm6CEKxBA@mail.gmail.com>
In reply to#3368
On Fri, Apr 22, 2011 at 10:13 AM, Siratinee Sukachai
<ploy.sukachai@gmail.com> wrote:
> 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

>> h={:a=>42}
=> {:a=>42}
>> h.update(:b => 43)
=> {:b=>43, :a=>42}
>> h
=> {:b=>43, :a=>42}
>>

or the constructive, in which the receiver is not modified, with #merge

>> h={:a=>42}
=> {:a=>42}
>> h.merge(:b => 43)
=> {:b=>43, :a=>42}
>> h
=> {: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
> --
> Posted via http://www.ruby-forum.com/.
>
>



-- 
The 1,000,000th fibonacci number contains '42' 2039 times; that is
almost 30 occurrences more than expected (208988 digits).
N.B. The 42nd fibonacci number does not contain '1000000' that is
almost the expected 3.0e-06 times.

[toc] | [prev] | [next] | [standalone]


#3377

FromSiratinee Sukachai <ploy.sukachai@gmail.com>
Date2011-04-22 05:43 -0500
Message-ID<3f091ca2771edbc2dcb14ad3d7f1779a@ruby-forum.com>
In reply to#3362
thanks Robert, it works.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web