Can I append hash to hashes?

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/.

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/.

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

···

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...

thanks Robert, it works.

···

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

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/.

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

···

On Fri, Apr 22, 2011 at 10:13 AM, Siratinee Sukachai <ploy.sukachai@gmail.com> wrote:

--
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.