Skip to content

Instantly share code, notes, and snippets.

@Meekohi
Created June 5, 2015 20:15
Show Gist options
  • Save Meekohi/841c1bca19df0977d44a to your computer and use it in GitHub Desktop.
Save Meekohi/841c1bca19df0977d44a to your computer and use it in GitHub Desktop.
# Old code
i = 0
@views.each do |view|
while (view[1].to_s != (@startDate+i).to_s) && (i < 30) do
#if no data for that day put 0 views in
@monthViewArray.push(0)
i+=1
end
#otherwise put in the number of views
@monthViewArray.push(view[0])
i+=1
end
# new, even more awesome code
@monthViewArray = (30.days.ago.to_date..Date.today).map{ |date| # go through each of the last 30 days
row = @views.find {|row| row[1].to_s==date.to_s} # find a row with this date (returns nil if there isn't one)
row.nil? ? 0 : row[0] # if you found a row use it, otherwise use 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment