Here''s my problem: I have an array of shipments, each of them has a ship_date @shipments = Shipment.find(:all, :order => [''ship_date ASC'']) I have an array of dates for which I will display scheduled shipments: @date_range = (start_date..end_date).to_a I am trying to create a hash that uses each scheduled date as the key and whose value is an array of shipments scheduled for that date. I get the array of scheduled ship dates like this: valid_dates = @shipments.collect {|s| s.ship_date}.uniq! Then proceed to build my nested hash like this: @ship_hash = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) } valid_dates.each do |date| @ship_hash[date][@shipments.select {|event| event.ship_date =date}] end What this @ship_hash looks like is this: {Tue, 29 Jul 2008=>{[#<Shipment_1>, #<Shipment_2>]=>{}, Thur, 31 Jul 2008=>{[#<Shipment_3>]=>{}}} My code is inserting a blank hash at the end of each value... I don''t understand Ruby enough to find it after squinting and Googling for some time. What I''d like it to look like is: {Tue, 29 Jul 2008=>[#<Shipment_1>, #<Shipment_2>], Thur, 31 Jul 2008=>[#<Shipment_3>]} Any help is much appreciated... thanks! SH --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > > I get the array of scheduled ship dates like this: > > valid_dates = @shipments.collect {|s| s.ship_date}.uniq! >Warning: uniq! will not always return the uniqued array (since it does the modification in place): [1,2,3].uniq! #=> nil [1,2,3,3].uniq! #=> [1,2,3]> Then proceed to build my nested hash like this: > > @ship_hash = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) } > valid_dates.each do |date| > @ship_hash[date][@shipments.select {|event| event.ship_date => date}] > endIsn''t there an assignment missing here? Your hash default proc means that saying @ship_hash[date] will create a new empty hash if needed, but then did you mean @ship_hash[date] = @shipments.select {|event| event.ship_date ==date}] Fred> > > What this @ship_hash looks like is this: > > {Tue, 29 Jul 2008=>{[#<Shipment_1>, #<Shipment_2>]=>{}, Thur, 31 > Jul 2008=>{[#<Shipment_3>]=>{}}} > > My code is inserting a blank hash at the end of each value... I don''t > understand Ruby enough to find it after squinting and Googling for > some time. What I''d like it to look like is: > > {Tue, 29 Jul 2008=>[#<Shipment_1>, #<Shipment_2>], Thur, 31 Jul > 2008=>[#<Shipment_3>]} > > Any help is much appreciated... thanks! > > SH > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the reply, Fred. I''ll add a condition to check if there are no duplicates in the array to avoid the uniq! gotcha. The assignment you suggested worked like a peach. Thanks! SH --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 31 Jul 2008, at 16:04, shenry wrote:> > Thanks for the reply, Fred. > > I''ll add a condition to check if there are no duplicates in the array > to avoid the uniq! gotcha.That wasn''t what I was suggesting. What I meant was that valid_dates = @shipments.collect {|s| s.ship_date}.uniq is ok, as is valid_dates = @shipments.collect {|s| s.ship_date} valid_dates.uniq! but valid_dates = @shipments.collect {|s| s.ship_date}.uniq! isn''t. Fred> > > The assignment you suggested worked like a peach. Thanks! > > SH > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Understood... clearly I need a Ruby primer (any suggestions?) Thanks again Fred. SH On Jul 31, 8:34 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 31 Jul 2008, at 16:04, shenry wrote: > > > > > Thanks for the reply, Fred. > > > I''ll add a condition to check if there are no duplicates in the array > > to avoid the uniq! gotcha. > > That wasn''t what I was suggesting. What I meant was that > > valid_dates = @shipments.collect {|s| s.ship_date}.uniq > is ok, as is > valid_dates = @shipments.collect {|s| s.ship_date} > valid_dates.uniq! > > but > valid_dates = @shipments.collect {|s| s.ship_date}.uniq! > > isn''t. > > Fred > > > > > The assignment you suggested worked like a peach. Thanks! > > > SH--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---