Skip to content

Instantly share code, notes, and snippets.

@alx
Created May 23, 2012 08:31
Show Gist options
  • Select an option

  • Save alx/2773915 to your computer and use it in GitHub Desktop.

Select an option

Save alx/2773915 to your computer and use it in GitHub Desktop.
#
# input:
#
# >> log
# => [{:time=>201201, :x=>2}, {:time=>201201, :y=>7}, {:time=>201201, :z=>2}, {:time=>201202, :a=>3}, {:time=>201202, :b=>4}, {:time=>201202, :c=>0}]
#
result = {}
log.each do |line|
result[line[:time]] = result[line[:time]].nil? ? line : result[line[:time]].merge(line)
end
result.values
#
# output:
#
# >> result
# => {201201=>{:time=>201201, :x=>2, :y=>7, :z=>2}, 201202=>{:time=>201202, :a=>3, :b=>4, :c=>0}}
#
# >> result.values
# => [{:time=>201201, :x=>2, :y=>7, :z=>2}, {:time=>201202, :a=>3, :b=>4, :c=>0}]
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment