Trying to turn this into that, why the box keeps red for the code below?
log.reduce([]) do |s,e|
r = s.last
(r = {}; s << r) if r.nil? or r[:time] != e[:time]
r.merge! e
s
end
It works fine though
$ irb
>> 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}
>> ]
=> [{:time=>201201, :x=>2}, {:time=>201201, :y=>7}, {:time=>201201, :z=>2}, {:time=>201202, :a=>3}, {:time=>201202, :b=>4}, {:time=>201202, :c=>0}]
>> log.reduce([]) do |s,e|
?> r = s.last
>> (r = {}; s << r) if r.nil? or r[:time] != e[:time]
>> r.merge! e
>> s
>> end
=> [{:time=>201201, :x=>2, :y=>7, :z=>2}, {:time=>201202, :a=>3, :b=>4, :c=>0}]
@ryenus, the quiz check uses a slightly different test data. Just play around with the order of hashes in the
log
array and you'll see that your method fails to solve the task in certain cases.