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/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, > BuvanaBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 December 2010 06:09, Priya Buvan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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... -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@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. Thanks, Buvana -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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} On Thu, Dec 9, 2010 at 10:26 AM, Priya Buvan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @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. > > Thanks, > Buvana > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/> On Thu, Dec 9, 2010 at 10:26 AM, Priya Buvan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> @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. >> >> Thanks, >> Buvana-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Sent from my iPhone > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Pankowecki (rupert)
2010-Dec-11 10:21 UTC
Re: Merging multiple hashes as single hash
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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.