in order to get the last four elements of your hash, you should first map
it as an array, get the indexes desired and then transform again the array into an hash.
For example:
2.2.1 :001 > hash = {a: 1, b: 2, c: 3, d: 4, e: 5}
=> {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}
2.2.1 :002 > hash.map{|h| h}[-4..-1].to_h
=> {:b=>2, :c=>3, :d=>4, :e=>5}
In your specific case, the code might look like this:
metric.sort.map{|h| h}[-4..-1].to_h.each do |key, value|
top_point = { x: Time.parse(key).to_time.to_i, y: value['top_10'] }
top_points << top_point
average_point = { x: Time.parse(key).to_time.to_i, y: value['average'] }
average_points << average_point
end
Another way to write it could be:
last_four_metrics = metric.sort.map{|h| h}[-4..-1].to_h
top_points = last_four_metrics.map{|k, v| { x: Time.parse(k).to_time.to_i, y: v['top_10'] }}
average_points = last_four_metrics.map{|k, v| { x: Time.parse(k).to_time.to_i, y: v['average'] }}
Update: compatibility with Ruby 1.9
last_four_metrics = Hash[ metric.sort.map{|h| h}[-4..-1] ]
top_points = last_four_metrics.map{|k, v| { x: Time.parse(k).to_time.to_i, y: v['top_10'] }}
average_points = last_four_metrics.map{|k, v| { x: Time.parse(k).to_time.to_i, y: v['average'] }}