From: Aldric Giacomoni Date: 2009-12-11T03:56:17+09:00 Subject: Re: Fill gaps in Array of Hashes Joao Silva wrote: > Aldric Giacomoni wrote: >> Joao Silva wrote: >>> I have the following object as a result from MySQL: >>> >>> [{:positive=>"0", :neutral=>"2", :total=>"5", :date=>"2009-12-01", >>> :negative=>"3"}, {:positive=>"3", :neutral=>"3", :total=>"7", >>> :date=>"2009-12-08", :negative=>"1"}, {:positive=>"1", :neutral=>"1", >>> :total=>"3", :date=>"2009-12-09", :negative=>"1"}] >>> >>> I want to fill gaps (for example from 2009-11-19 till 2010-10-01) with >>> {:positive=>"0", :neutral=>"0", :total=>"0", :date=>"HEREDATE", >>> :negative=>"1" - how i can do this in ruby? In MySQL it's impossible to >>> do :-(. >> >> Alright... Now that I've helped you do what you asked for, please tell >> us what you actually want to do, because actually filling up a hash with >> default data is really not optimal. > > I don't have choice: I need data formed like this to generate report > (chart) data. And i need for each day approporiate values. You could check whether the data is in the array, and return the default that you want if it's not in there. Pseudo code: From date1 to date2, do |date| data = array.find { |x| x[:date] == date } if data.nil? data = { your default hash } end do_whatever_you_want_with_data_here end Why do you have an array of hashes, anyway? Why not a hash with the dates as keys and the rest of the hash as values? a hash of hashes. -- Posted via http://www.ruby-forum.com/.