Skip to content

Instantly share code, notes, and snippets.

@costis
Created May 28, 2013 10:38
Show Gist options
  • Save costis/5661903 to your computer and use it in GitHub Desktop.
Save costis/5661903 to your computer and use it in GitHub Desktop.
Task from Alek
log = [
{time: 201202, a: 3},
{time: 201201, y: 7},
{time: 201201, z: 2},
{time: 201202, b: 4},
{time: 201202, c: 0},
{time: 201201, x: 2}
]
log.inject([]) do |acc, item|
idx = acc.find_index { |x| x[:time] == item[:time] }
idx ? acc[idx].merge!(item.keep_if{ |k,v| k != :time }) : acc << item
acc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment