Merging multiple hashes as single hash

230 views
Skip to first unread message

Priya Buvan

unread,
Dec 9, 2010, 1:09:52 AM12/9/10
to rubyonra...@googlegroups.com
Hi All,

I'm having unknown number of hashes. How to merge all those hashes into
single hash?

Thanks,
Buvana

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

Marnen Laibow-Koser

unread,
Dec 9, 2010, 1:18:08 AM12/9/10
to rubyonra...@googlegroups.com
Priya Buvan wrote in post #967340:

> Hi All,
>
> I'm having unknown number of hashes. How to merge all those hashes into
> single hash?

Think about the semantics here! What happens if the same key appears in
all the hashes? Which value takes priority?

You can use inject to merge the hashes, but you probably won't get the
desired result.

>
> Thanks,
> Buvana

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Sent from my iPhone

Michael Pavling

unread,
Dec 9, 2010, 2:01:53 AM12/9/10
to rubyonra...@googlegroups.com
On 9 December 2010 06:09, Priya Buvan <li...@ruby-forum.com> wrote:
> I'm having unknown number of hashes. How to merge all those hashes into
> single hash?

use "merge"... http://ruby-doc.org/core/classes/Hash.html#M002880


When you say you have "unknown number", are they in an array? if so,
iterate it an merge them...

Priya Buvan

unread,
Dec 9, 2010, 2:26:42 AM12/9/10
to rubyonra...@googlegroups.com
@Michael: yes its in array..

@Marnen: If there is more than one key with same name, obviously it'll
be override. This is fine.

Anyway i'm trying to fetch the result separately. I'll reply with latest
updated soon.

Vladimir Rybas

unread,
Dec 9, 2010, 7:09:10 AM12/9/10
to rubyonra...@googlegroups.com
array_of_hashes = [{:first => 1, :second => 2}, {:first => 10, :third => 3}]

Hash[*array_of_hashes.collect{|hash| hash.collect{|key,value|
[key,value].flatten}.flatten}.flatten]
=> {:first=>10, :second=>2, :third=>3}

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

Rob Biedenharn

unread,
Dec 9, 2010, 2:11:45 PM12/9/10
to rubyonra...@googlegroups.com

On Dec 9, 2010, at 7:09 AM, Vladimir Rybas wrote:

> array_of_hashes = [{:first => 1, :second => 2}, {:first =>
> 10, :third => 3}]
>
> Hash[*array_of_hashes.collect{|hash| hash.collect{|key,value|
> [key,value].flatten}.flatten}.flatten]
> => {:first=>10, :second=>2, :third=>3}

Simpler, but same idea.

irb> Hash[*array_of_hashes.map{|_|_.to_a}.flatten]
=> {:second=>2, :third=>3, :first=>10}

-Rob

Rob Biedenharn
R...@AgileConsultingLLC.com http://AgileConsultingLLC.com/
r...@GaslightSoftware.com http://GaslightSoftware.com/

Vladimir Rybas

unread,
Dec 10, 2010, 4:51:53 AM12/10/10
to rubyonra...@googlegroups.com
> You can use inject to merge the hashes, but you probably won't get the
> desired result.

Actually, looks like you get the desired result

a = [{:first => 1, :second => 2}, {:first => 10, :third => 3}]
a.inject(:merge)

On Thu, Dec 9, 2010 at 9:18 AM, Marnen Laibow-Koser
<li...@ruby-forum.com> wrote:
> Priya Buvan wrote in post #967340:
>> Hi All,
>>
>> I'm having unknown number of hashes. How to merge all those hashes into
>> single hash?
>
> Think about the semantics here!  What happens if the same key appears in
> all the hashes? Which value takes priority?
>
>
>>

>> Thanks,
>> Buvana
>
> Best,
> --
> Marnen Laibow-Koser
> http://www.marnen.org
> mar...@marnen.org
>
> Sent from my iPhone
>
> --
> Posted via http://www.ruby-forum.com/.
>

Robert Pankowecki (rupert)

unread,
Dec 11, 2010, 5:21:44 AM12/11/10
to Ruby on Rails: Talk
In case you need something more smart in the future to deal with same
keys in hashes:

http://rubyworks.github.com/facets/doc/api/core/Hash.html#method-i-weave

Robert Pankowecki
http://robert.pankowecki.pl
Reply all
Reply to author
Forward
0 new messages